您好我正在创建一个应用程序,它会定期在特定时间间隔内发出警报。它运作良好。但当我关闭我的手机并再次打开警报不工作。请帮助解决问题。
我的闹钟代码是:
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
答案 0 :(得分:2)
在AndroidManifest.xml中添加:
<receiver android:name=".BootCompletedIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
和
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
添加此课程:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
答案 1 :(得分:1)
创建一个BroadCastReceiver
并调用此警报代码将此警报代码放入此接收器并向该接收器添加启动完成权限,这样当您的电话打开时,该接收器将自动调用。请参阅:
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
}
}
AndroidManifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package_name" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>