无法获取android通知的完整工作代码
我想在一段时间间隔后自动显示和android通知 我已经使用了以下无法获取android通知的完整工作代码,android,android-layout,android-intent,android-notifications,Android,Android Layout,Android Intent,Android Notifications,我想在一段时间间隔后自动显示和android通知 我已经使用了以下所有建议的代码片段,但我无法在5分钟后获得自动通知 public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String message = "Hellooo, alrm worked ----";
所有建议的代码片段
,但我无法在5分钟后获得自动通知
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = "Hellooo, alrm worked ----";
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(context, TripNotification.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
public void setAlarm(Context context){
Log.d("Carbon","Alrm SET !!");
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 30 seconds to the calendar object
cal.add(Calendar.SECOND, 30);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
}
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
公共类AlarmReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
String message=“Hellooo,alrm已工作----”;
Toast.makeText(上下文、消息、Toast.LENGTH_SHORT).show();
Intent intent2=新意图(上下文,TripNotification.class);
intent2.setFlags(Intent.FLAG\u活动\u新任务);
背景。起始触觉(意图2);
}
公共无效设置报警(上下文){
Log.d(“碳”,“Alrm集合!!”);
//获取具有当前时间的日历对象
Calendar cal=Calendar.getInstance();
//向日历对象添加30秒
计算添加(日历秒,30);
意向意向=新意向(上下文,AlarmReceiver.class);
PendingEvent发送方=PendingEvent.getBroadcast(上下文,192837,意图,PendingEvent.FLAG_UPDATE_CURRENT);
//获取AlarmManager服务
AlarmManager am=(AlarmManager)context.getSystemService(context.ALARM\u服务);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),发送方);
}
}
我建议您编写一个活动。在onCreate()方法中每5分钟设置一次报警,如下所示:
Intent intent = new Intent(this, ServiceForAlarm.class);
intent.setAction(""+System.currentTimeMillis());
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(m_Context, 0, intent, Intent.FLAG_GRANT_READ_URI_PERMISSION);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 300000L, pendingIntent);
在ServiceForAlarm
中,编写代码以推送通知
public class ServiceForAlarm extends Service {
private Context mContext;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mContext = getApplicationContext();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
SetNotification();
stopSelf();
}
private void SetNotification() {
m_NotificationManager = (NotificationManager)m_Context.getSystemService(Context.NOTIFICATION_SERVICE);
m_Notification = new Notification(R.drawable.icon_app, "Your message", System.currentTimeMillis());
m_Intent = new Intent() ;
m_PendingIntent = PendingIntent.getActivity(m_context, 0, m_Intent, 0);
m_Notification.contentIntent = m_PendingIntent;
m_Notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
m_Notification.setLatestEventInfo(m_context, "Title", "Click me", m_PendingIntent);
m_NotificationManager.notify(1010, m_Notification);
}
}您应该使用广播接收器
和广播意图
而不是活动
。非常感谢,但是如何使用呢?我不熟悉安卓系统。您能给我一些可用的代码片段吗?请检查pendingent
和AlarmManager
文档。您只需要在代码中创建一个广播挂起意图并创建一个BroadcastReceiver
,然后在清单中声明。然后,您的接收者可以更新通知。我确实查看了文档,但不知道在哪里调用代码snipet:(您的接收者注册的目的过滤器是什么?是否有完整的代码片段?ServiceForAlarm的代码在哪里?