AlarmManager的setRepeating()更新apk版本码后不重复

setRepeating() of AlarmManager does not repeat after update apk Versioncode

setRepeating() 更新apk后AlarmManager不重复 Versioncode

有什么方法可以在更新 apk 后立即自动 运行 吗?
时间已经在以前的版本代码上设置,但警报仅在 phone 重新启动时起作用。

//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();

class 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);
}

非常感谢

刚刚解决,版本更新后必须重启服务。添加清单:

<receiver android:name=".Launcher" >
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
    </intent-filter>
</receiver>

启动器Class

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));

    }
}