开发者

services using alarm manager

开发者 https://www.devze.com 2023-03-05 10:30 出处:网络
This is my 开发者_开发问答code, but it does not work. In settings activtiy: AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号