在我的代码中,我在Android清单文件中添加了Boot Completed的所有权限。我记录了我的广播接收器,重启后控件进入广播接收器但是警报没有启动。我想要的是一个活动触发报警后启动。正常情况下(关机前)我收到报警并启动活动。重启后没有发生......
这是我的Boadcast接收器
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 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
$
答案 0 :(得分:1)
我遇到了问题
在未决意图中,我使用了要启动的活动。但是故障......在未决意图中你应该使用你的广播接收器作为意图...
if (sp.getBoolean(BigBirdConstants.pref_alarm_set, false)) {
String alrm = sp.getString(
BigBirdConstants.pref_myflight_alarm_time, "hello");
String alrmtime[] = new String[2];
alrmtime = alrm.split(":");
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
/*
* Integer.parseInt(alrmtime[
* 0])
*/
/* Integer.parseInt(alrmtime[1]) */
cal.set(Calendar.HOUR_OF_DAY,11);
cal.set(Calendar.MINUTE,13);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent1 = new Intent(context,
AlarmReceiver.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, 192837, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("hai",
"alarm set");