How to Force Stop My Android Application Programmatically

How can I programmatically force stop an Android app with Java?

get the process ID of your application, and kill that process onDestroy() method

@Override
public void onDestroy()
{
super.onDestroy();

int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}

or

getActivity().finish();
System.exit(0);

and if you want to kill other apps from your activity, then this should work

You can send the signal using:

Process.sendSignal(pid, Process.SIGNAL_KILL);

To completely kill the process, it's recommended to call:

ActivityManager.killBackgroundProcesses(packageNameToKill)

before sending the signal.

Please, note that your app needs to own the KILL_BACKGROUND_PROCESSES permission. Thus, in the AndroidManifest.xml, you need to include:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

Force Close an app programmatically

I found a solution:

Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

os.writeBytes("adb shell" + "\n");

os.flush();

os.writeBytes("am force-stop com.xxxxxx" + "\n");

os.flush();

Where com.xxxxxx is package name of application to force stop.

How to close an android application?

I found my solution. Use this to close an application

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

how to force stop android service programmatically

Here's a simplified description of how to stop services :

stopSelf() is used to always stop the current service.

stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started.

stopService(Intent service) is used to stop services, but from outside the service to be stopped.

visit this link for more details

please replace

return START_STICKY;

by

return START_NOT_STICKY;

Difference:

START_STICKY

the system will try to re-create your service after it is killed

START_NOT_STICKY

the system will not try to re-create your service after it is killed



Related Topics



Leave a reply



Submit