Android 即使重新启动后,警报管理器是否仍然存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12034357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
does Alarm Manager persist even after reboot?
提问by Xelamae
I am really new to android, I have been researching about alarms. I want to alarm if there is a birthday on that day. I've have used alarm manager. I was confused because i have read that it clears after reboot. I don't have an android phone so I'm just using the emulator.
我对android真的很陌生,我一直在研究警报。如果那天有生日,我想报警。我用过闹钟管理器。我很困惑,因为我读过它在重新启动后清除。我没有安卓手机,所以我只是使用模拟器。
Here's my code :
这是我的代码:
public void schedAlarm() {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmService.class);
pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}
I made this BroadcastRecever in replace for AlarmSerivce Here :
我用这个 BroadcastRecever 代替了 AlarmSerivce 在这里:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "It Birthday!";
CharSequence message =" Greet your friend.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher, "Birthday", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
is this enough??
这够了吗??
回答by Lucifer
A simple answer would be NO. But yes you can achieve this by creating a BroadCastReceiver
which will start the Alarm while booting completes of the device.
一个简单的答案是NO。但是是的,您可以通过创建一个BroadCastReceiver
在设备启动完成时启动警报来实现这一点。
Use <action android:name="android.intent.action.BOOT_COMPLETED" />
for trapping boot activity in BroadCastReceiver class.
使用<action android:name="android.intent.action.BOOT_COMPLETED" />
在广播接收器类诱捕启动活动。
You need to add above line in AndroidManifest.xml as follows,
您需要在 AndroidManifest.xml 中添加上一行,如下所示,
<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
回答by Mohan Munisifreddy
Yes , you can make AlarmManager to work even after rebooting. Perhaps this is the easiest way : add the below code in your AndroidManifest.xml:
是的,即使在重新启动后,您也可以使 AlarmManager 工作。也许这是最简单的方法:在您的 AndroidManifest.xml 中添加以下代码:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
don't forget to include user-permission to the AndroidManifest.xml as:
不要忘记在 AndroidManifest.xml 中包含用户权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
回答by Romy Gomes
in some phones only adding
在某些手机中只添加
<action android:name="android.intent.action.Boot_COMPLETED" />
does not work you also have to add
不起作用,您还必须添加
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
along with previous one
连同上一个