在我的代码中,我在 Android 清单文件上添加了 Boot Completed 的所有权限。我登录了我的广播接收器,重新启动后控件将进入广播接收器但警报没有触发。实际上我想要的是一个活动在之后启动触发警报..在正常情况下(关闭前)我收到警报并启动了活动..重新启动后它没有发生......
这是我的广播接收器
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("hai", "inside onreceive");
try {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,9);
cal.set(Calendar.MINUTE,49);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent1 = new Intent(context,
AlarmLauncher.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.putExtra("alarm_message", "feathersoft Rules!");
// In reality, you would want to have a static variable
// for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent1,PendingIntent.FLAG_CANCEL_CURRENT);
Log.d("hai",
"alarm set");
// Get the AlarmManager service
AlarmManager am = (AlarmManager) context
.getSystemService(context.ALARM_SERVICE);
// am.set(AlarmManager.RTC_WAKEUP,
// cal.getTimeInMillis(), sender);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
sender);
Log.d("hai",
"Alarm Alert:Inside the boot completed broadcast receiver......");
Log.d("hai",
"Alarm Alert:Inside the boot completed broadcast receiver......");
}
}
}catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try {
if(intent.getAction().equals(null))
{
SharedPreferences sp = context
.getSharedPreferences(
BigBirdConstants.nameOfPreference,
context.MODE_PRIVATE);
String alrm = sp.getString(
BigBirdConstants.pref_myflight_alarm_time, "hello");
Log.d("hai", alrm);
Intent newIntent = new Intent(context, AlarmLauncher.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
System.out.println("boot completed");
// newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我重启后的警报 dumpsys
$ dumpsys alarm
dumpsys alarm
Current Alarm Manager state:
Realtime wakeup (now=1334031884759):
RTC_WAKEUP #2: Alarm{460a6c88 type 0 com.android.providers.calendar}
type=0 when=1334110692157 repeatInterval=0 count=0
operation=PendingIntent{45dea300: PendingIntentRecord{460a6c10 com.android.p
roviders.calendar broadcastIntent}}
RTC_WAKEUP #1: Alarm{460eb1d0 type 0 com.google.android.gsf}
type=0 when=1334073656706 repeatInterval=566387000 count=0
operation=PendingIntent{45d0d9c8: PendingIntentRecord{4606f3c0 com.google.an
droid.gsf broadcastIntent}}
RTC_WAKEUP #0: Alarm{4610cd88 type 0 com.google.android.gsf}
type=0 when=1334032080140 repeatInterval=1800000 count=0
operation=PendingIntent{46057dc0: PendingIntentRecord{4610cd10 com.google.an
droid.gsf broadcastIntent}}
RTC #1: Alarm{45ed7b10 type 1 android}
type=1 when=1334082600000 repeatInterval=0 count=0
operation=PendingIntent{45dd2c10: PendingIntentRecord{45e81c08 android broad
castIntent}}
RTC #0: Alarm{461bf460 type 1 android}
type=1 when=1334031900000 repeatInterval=0 count=0
operation=PendingIntent{45cceb98: PendingIntentRecord{45f2b3e0 android broad
castIntent}}
Elapsed realtime wakeup (now=527752):
ELAPSED_WAKEUP #2: Alarm{46169c28 type 2 android}
type=2 when=25881048 repeatInterval=0 count=0
operation=PendingIntent{45d0c358: PendingIntentRecord{45cc99b0 android broad
castIntent}}
ELAPSED_WAKEUP #1: Alarm{4610dc40 type 2 com.google.android.gsf}
type=2 when=995077 repeatInterval=0 count=0
operation=PendingIntent{460dd5c8: PendingIntentRecord{4610dbc8 com.google.an
droid.gsf broadcastIntent}}
ELAPSED_WAKEUP #0: Alarm{46011b68 type 2 com.google.android.apps.maps}
type=2 when=970384 repeatInterval=900000 count=0
operation=PendingIntent{45f9f478: PendingIntentRecord{45fea1d0 com.google.an
droid.apps.maps startService}}
ELAPSED #1: Alarm{460da648 type 3 android}
type=3 when=2557073343 repeatInterval=0 count=0
operation=PendingIntent{45f84d38: PendingIntentRecord{45e34428 android broad
castIntent}}
ELAPSED #0: Alarm{4609a418 type 3 android}
type=3 when=727667 repeatInterval=0 count=0
operation=PendingIntent{45f7da98: PendingIntentRecord{45e39420 android broad
castIntent}}
Broadcast ref count: 0
Alarm Stats:
com.bigbird.Activity
20ms running, 1 wakeups
1 alarms: flg=0x10000004 cmp=com.bigbird.Activity/.AlarmLauncher
com.google.android.gsf
47ms running, 1 wakeups
1 alarms: act=com.google.android.intent.action.GTALK_RECONNECT flg=0x4
android
22819ms running, 3 wakeups
1 alarms: act=com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KE
YGUARD flg=0x4
8 alarms: act=android.intent.action.TIME_TICK flg=0x40000004
2 alarms: act=android.content.syncmanager.SYNC_ALARM flg=0x4
$