Java 报警管理器无法设置重复
我对Android中的Alarm Manager有一些问题 在onCreate方法中:Java 报警管理器无法设置重复,java,android,alarmmanager,Java,Android,Alarmmanager,我对Android中的Alarm Manager有一些问题 在onCreate方法中: notificationCount = notificationCount + 1; AlarmManager mgr = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); Intent notificationIntent = new Intent(
notificationCount = notificationCount + 1;
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
ReminderAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context,
notificationCount, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
60000+System.currentTimeMillis(), pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
提醒报警等级:
public class ReminderAlarm extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0, new Intent(), 0);
notification = new Notification(
R.drawable.ic_launcher, "Notification",
System.currentTimeMillis());
notification
.setLatestEventInfo(context, "AlarmActivated", "Hello", contentIntent);
mNotificationManager.notify(
Integer.parseInt(intent.getExtras()
.get("NotifyCount").toString()),
notification); }}
在引导接收器类中:
public void onReceive(Context context, Intent i) {
scheduleAlarms(context);
}
static void scheduleAlarms(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, ReminderAlarm.class);
PendingIntent pi = PendingIntent.getService(context, 0,
notificationIntent, 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
60000+System.currentTimeMillis(),
pi);
}
在我的清单文件中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".ReminderAlarm" >
</receiver>
<receiver
android:name=".BootReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
因此,通过将重复设置为这样,它在44年后是否仍会像前一个一样运行?或者它将在每天上午12点重复?方法中的第三个参数是以毫秒为单位的间隔。您将其设置为自1970年1月1日00:00:00.0 UTC加60000以来经过的毫秒数,使下一个计划的事件大约在44年后发生。按如下方式更改代码:
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);
比我快2秒:D@MikeM. 谢谢!它起作用了!但我想知道是否要将其设置为每天运行一次,我应该将代码替换为:mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_day,pi)@米肯。我不敢相信事情会这么简单,直到我开始写答案,然后……哦,等等……:D@MikeM. 好的,谢谢!但我确实有一些问题。你能帮我看一下编辑过的部分吗?@MikeM。对不起,它会像我期望的那样运行吗?或者我该如何解决这个问题?
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);