How to programmatically force a full restart of an application? like kill and then start

I have an application that includes both a comprehensive interface and activity. The application works on several devices that communicate via WIFI through the service.

Since the application is a prototype of / in -development, I want to add support for a "reboot", which will kill the application and restart it. Depending on the use case, there are many common user interface files, and during testing it would be easier during testing (I have several devices) if I could just click a button to completely restart the application.

So, does anyone have any suggestions on how to:

1) Force close / stop / kill your own application from your application.

2) Set the timer / intent that the OS tells you to start the application before closing / stop / kill it.

Any advice would be appreciated! Thanks.

+4
source share
6 answers

Use the code below to restart the application:

Intent mStartActivity = new Intent(HomeActivity.this, SplashScreen.class); int mPendingIntentId = 123456; PendingIntent mPendingIntent = PendingIntent.getActivity(HomeActivity.this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager) HomeActivity.this.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); System.exit(0); 
+7
source

As you can see, finish () is what you want to use to kill activity. The ACR path will work, however it will only restart your activity, and not really kill the process and start it. If this is what you are looking for, instead of having a dummy activity that restarts the original activity, using flags is the right way to do this.

 Intent i = new Intent(this, WrapperActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); 

These flags will clear your back stack right up to the first instance of any activity you create, it will kill it, and then create a new one. This is essentially what the ACR example does, but it is much more concise.

If this is not enough for you, in order to do it right, this is quite a lot of work and requires more in-depth knowledge of the Android system. You want to create a service that runs in the background (there must be a separate process if you want the application level state to be killed) that you could launch when you want to kill the application, ask the application to kill itself or the service will kill the application for you, and then start the launch of the service, which will start your activity / application.

Hope this helps! Good luck

+2
source

Try the code below to restart the application.

 Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage(getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); 
+1
source

No. The operating system decides when to kill the application. Your application can come back to life, whether it was really dead or just hidden.

This is inconvenient, but the platform works like this. You can create the same custom effect by controlling your actions, which you can kill and restore.

0
source

There may be a more formal way of doing this, but here's how I would do it.

For example, I'm going to pretend that there are only two actions, the one you are currently in (I will call it FirstActivity), and another β€œauxiliary” activity (I will call SecondActivity).

In the first (the one you want to restart), you will have a button that initiates the restart of the application.

 restartButton.setOnClickListener(new OnClickListener(){ @Override onClick(View v){ //when clicked it starts the helper activity and closes the one you're in startActivity(new Intent(this, SecondActivity.class)); finish(); //or you could use FirstActivity.onDestroy(); if you want it completely dead } }); 

Second activity: the whole goal is that you can basically restart the application from your application (close everything else and then restart it in this)

 Class SecondActivity extends Activity{ @Override onCreate(Bundle savedInstanceState){ ... //it restarts the old activity so it new and ends this one startActivity(new Intent(this, FirstActivity.class)); finish(); //or you could use SecondActivity.onDestroy(); if you want it } } 

This will completely restart the first action. I'm not sure if it will be as thorough as you want, but there is no other way to do such things AFAIK.

0
source

Here is what you need to do:

  • Create a sticky service
  • Kill the application with call killProcess from the service
  • Then the sticky service will restart and you will be able to open the application with the intention of getLaunchIntentForPackage

I like to use a handler for this from the main user interface thread:

 private void scheduleForceClose() { final Handler closeAppHandler = new Handler(); closeAppHandler.post(new Runnable() { @Override public void run() { prefs.edit().putBoolean(StringConstants.FORCE_CLOSED_APP, true).commit(); Log.i(TAG, "Gonna force kill the app process completely!"); System.exit(0); android.os.Process.killProcess(android.os.Process.myPid()); } }); 

}

 private void scheduleForceOpen() { final Handler openAppHandler = new Handler(); taskOpenApp = new TimerTask() { @Override public void run() { openAppHandler.post(new Runnable() { @SuppressLint("ApplySharedPref") @Override public void run() { Intent intent = getPackageManager().getLaunchIntentForPackage("com.company.yourpackagename"); // Reset the force-close flag so that the close timer can begin again prefs.edit().putBoolean(StringConstants.FORCE_CLOSED_APP, false).commit(); startActivity(intent); } }); } }; // Decide whether we already force-closed the app, so that we don't repeat it boolean alreadyForceClosed = prefs.getBoolean(StringConstants.FORCE_CLOSED_APP, false); if (alreadyForceClosed) { Log.i(TAG, "App process has already been killed, so schedule app relaunch."); Timer timerOpen = new Timer(); timerOpen.schedule(taskOpenApp, 5000 /* reopen the app after 5 sec */); } } 
0
source

Source: https://habr.com/ru/post/1492774/


All Articles