确切时间重启?

时间:2018-01-29 14:18:12

标签: java android android-service alarmmanager android-pendingintent

我想让我的应用每天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");
    }

1 个答案:

答案 0 :(得分:0)

可能不是最佳解决方案,但它有效:

如果currentTime等于restartTime,我每隔一分钟运行另一个服务并重新启动。