警报在android中不起作用?
作者:互联网
我觉得我觉得非常微不足道.
基本上我在将来的某个特定时刻安排闹钟:
Intent contentIntent = new Intent(this, AlarmReceiver.class);
PendingIntent theappIntent = PendingIntent.getService(Main.this, 0,contentIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent);
移动设备不断开启时工作正常.
我的问题是重启移动设备后不会触发警报
并在预期时间开启.
我错过了什么吗?我应该做一些额外的工作来保持警惕
重启后安排?
解决方法:
您必须在设备启动时重置警报.
当设备启动并重置警报时,您将丢失以检测应用程序.以下是在清单中编写的代码
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
还要创建一个BroadcastReceiver来接收启动完成的意图
public class YourReceiver extends BroadcastReceiver {
Context ct;
@Override
public void onReceive(Context context, Intent intent)
{
ct=context;
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
// Reset your alarm here
Intent contentIntent = new Intent(ct, AlarmReceiver.class);
PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute);
AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent);
}
}
标签:android,android-alarms,alarmmanager 来源: https://codeday.me/bug/20191003/1850205.html