在准确的时间重启?
Restart at exact time?
我想让我的应用程序 每天在 20:00 重新启动,但它没有按预期工作。第一次执行按计划进行,但之后 无休止地重新启动 。
我希望应用程序忘记 PendingIntent
并添加 PendingIntent.FLAG_ONE_SHOT
,但它没有任何效果。
我想,我错过了一个重要的标志,它在第一次执行后结束 PendingIntent
,但到目前为止找不到任何东西。
有什么想法吗?提前致谢!
App.java(有效)
// Create the calendar object
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Schedule the restart
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, RestartReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
RestartReceiver.java(部分有效)
public class RestartReceiver extends BroadcastReceiver {
static Context context;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Restart!", Toast.LENGTH_LONG).show(); // works one time
Util.restart(App.getInstance().getApplicationContext()); // executed endlessly
}
}
Util.java(从 here 复制 1:1,在 AlarmManager 未调用时有效)
try {
//check if the context is given
if (c != null) {
//fetch the packagemanager so we can get the default launch activity
// (you can replace this intent with any other activity if you want
PackageManager pm = c.getPackageManager();
//check if we got the PackageManager
if (pm != null) {
//create the intent with the default start activity for your application
Intent mStartActivity = pm.getLaunchIntentForPackage(
c.getPackageName()
);
if (mStartActivity != null) {
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//create a pending intent so the application is restarted after System.exit(0) was called.
// We use an AlarmManager to call this intent in 100ms
int mPendingIntentId = 223344;
PendingIntent mPendingIntent = PendingIntent
.getActivity(c, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
//kill the application
System.exit(0);
} else {
Log.e(TAG, "Was not able to restart application, mStartActivity null");
}
} else {
Log.e(TAG, "Was not able to restart application, PM null");
}
} else {
Log.e(TAG, "Was not able to restart application, Context null");
}
} catch (Exception ex) {
Log.e(TAG, "Was not able to restart application");
}
可能不是最好的解决方案,但它有效:
我有另一个每分钟运行一次的服务,如果 currentTime
等于 restartTime
则重新启动。
我想让我的应用程序 每天在 20:00 重新启动,但它没有按预期工作。第一次执行按计划进行,但之后 无休止地重新启动 。
我希望应用程序忘记 PendingIntent
并添加 PendingIntent.FLAG_ONE_SHOT
,但它没有任何效果。
我想,我错过了一个重要的标志,它在第一次执行后结束 PendingIntent
,但到目前为止找不到任何东西。
有什么想法吗?提前致谢!
App.java(有效)
// Create the calendar object
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Schedule the restart
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, RestartReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
RestartReceiver.java(部分有效)
public class RestartReceiver extends BroadcastReceiver {
static Context context;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Restart!", Toast.LENGTH_LONG).show(); // works one time
Util.restart(App.getInstance().getApplicationContext()); // executed endlessly
}
}
Util.java(从 here 复制 1:1,在 AlarmManager 未调用时有效)
try {
//check if the context is given
if (c != null) {
//fetch the packagemanager so we can get the default launch activity
// (you can replace this intent with any other activity if you want
PackageManager pm = c.getPackageManager();
//check if we got the PackageManager
if (pm != null) {
//create the intent with the default start activity for your application
Intent mStartActivity = pm.getLaunchIntentForPackage(
c.getPackageName()
);
if (mStartActivity != null) {
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//create a pending intent so the application is restarted after System.exit(0) was called.
// We use an AlarmManager to call this intent in 100ms
int mPendingIntentId = 223344;
PendingIntent mPendingIntent = PendingIntent
.getActivity(c, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
//kill the application
System.exit(0);
} else {
Log.e(TAG, "Was not able to restart application, mStartActivity null");
}
} else {
Log.e(TAG, "Was not able to restart application, PM null");
}
} else {
Log.e(TAG, "Was not able to restart application, Context null");
}
} catch (Exception ex) {
Log.e(TAG, "Was not able to restart application");
}
可能不是最好的解决方案,但它有效:
我有另一个每分钟运行一次的服务,如果 currentTime
等于 restartTime
则重新启动。