services using alarm manager
This is my 开发者_开发问答code, but it does not work.
In settings activtiy:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, OnBootReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+1000,
pendingIntent);
In BroadcastReceiver:
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "xyz";
CharSequence message = "Crazy About Android...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Settings.class), 0);
Notification notif = new Notification(R.drawable.icon, "Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
mgr.notify(1, notif);
If your are trying to start your intent in 1 second using the AlarmManager.RTC_WAKEUP
unit,
you might want to use System.currentTimeMillis()
instead of SystemClock.elapsedRealtime()
Differences are explained here :
http://developer.android.com/reference/android/os/SystemClock.html
AlarmManager.RTC_WAKEUP
must be used with currentTimeMillis()
;
Otherwise you can use the flag :
AlarmManager.ELAPSED_REALTIME_WAKEUP
to provide a time calculated with SystemClock.elapsedRealtime()
Flags are explained here : http://developer.android.com/reference/android/app/AlarmManager.html.
精彩评论