简体   繁体   中英

How to restart Android app in Android 10 after few seconds

I want to restart application after some seconds, following code worked in Android 8, but in Android 10 device nothing happens. Is ANY consideration is needed in Android 10? Here is the code worked in Android 8

Context mContext = getContext();
final int DELAY_I_WILL_BACK = 60;
. . .          
Intent mStartActivity = new Intent(mContext, MainActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, mPendingIntentId, mStartActivity, 
       PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + DELAY_I_WILL_BACK*1000, mPendingIntent);
System.exit(0);

I tried mActivity.finishActivity(1); just before System.exit(0) but still does not working.

Try this:-

private static final int MAGICAL_NUMBER = 2323 ;
private Handler handler = new Handler();
private static final long SCAN_PERIOD = 3000;

private void restartAppAfter10sec () {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                restartApp();
            }
        },SCAN_PERIOD);
}

private void restartApp() {
      Intent intent = new Intent(getContext(), MainActivity.class);
      int mPendingIntentId = MAGICAL_NUMBER;
      PendingIntent mPendingIntent = PendingIntent.getActivity(getContext(), 
      mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
      AlarmManager mgr = (AlarmManager) 
      getContext().getSystemService(Context.ALARM_SERVICE);
      mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, 
      mPendingIntent);
      System.exit(0);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM