Home > database >  Android - Kill the app from the app itself
Android - Kill the app from the app itself

Time:02-04

I have a teclast M40 pro tablet running Android Version (V11) with the app luncher to only boot one app. Now there is a problem, that sometimes the Wifi does not reconnect and a app restart is necessary. To prevent a manual restart, I would like to kill the app from the app itself and let it boot again with the app luncher.

So, how can I kill the app (Software Restart) from the app itself?

CodePudding user response:

You can use System.exit(0) to kill it and PendingIntent to start your application. Considering SplashActivity is your starting point.

Intent intent = new Intent(context, MainActivity.class);
int intentId = 123;
PendingIntent pendingIntent = PendingIntent.getActivity(context, intentId,    intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis()   100, mPendingIntent);
System.exit(0);

CodePudding user response:

you can use:

fun triggerRestart(context: Activity) {
  val intent = Intent(context, MainActivity::class.java)
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  context.startActivity(intent)
  if (context is Activity) {
     (context as Activity).finish()
  }
  Runtime.getRuntime().exit(0)
}

and for kotlin, replace

Runtime.getRuntime().exit(0)

with

kotlin.system.exitProcess(0)
  •  Tags:  
  • Related