setRepeating()
不会重复
有没有办法在更新apk后自动运行它? 时间已在先前的版本代码上设置,但警报仅在手机重新启动时有效。
//SET ALARM
Cal.setTimeInMillis(System.currentTimeMillis());
Cal.set(Calendar.HOUR_OF_DAY, hour);
Cal.set(Calendar.MINUTE, minute);
Cal.set(Calendar.SECOND, 0);
Editor e = sharedPrefs.edit();
e.putLong("userTime", Cal.getTimeInMillis());
e.commit();
mytime.setSummary(DateFormat.getTimeFormat(getBaseContext())
.format(new Date(Cal.getTimeInMillis())));
new AlarmTask(UserSettingActivity.this, Cal).run();
类AlarmTask实现了Runnable:
private final Calendar date;
private final AlarmManager am;
private final Context context;
public AlarmTask(Context context, Calendar date) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
}
@Override
public void run() {
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
非常感谢
答案 0 :(得分:0)
刚解决,版本更新后必须重新启动服务。添加清单:
<receiver android:name=".Launcher" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
启动器类
public class Launcher extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
context.stopService(new Intent(context.getApplicationContext(), NotifyService.class));
context.startService(new Intent(context.getApplicationContext(), NotifyService.class));
}
}