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
Android Sdk 22 - Searchview Rendering Problems
Refresh Fragment UI from Fragmentactivity
Quaternion Rotation Does Not Work as Excepted
How to Determine Which Dependency Causes Google Play Openssl Warning
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'
How to Have a Transparent Imagebutton: Android
How to Show an Empty View with a Recyclerview
Switching to Landscape Mode in Android Emulator
Android:Inapp Purchase Receipt Validation Google Play
How to Get Color-Int from Color Resource
Error: No Toolchains Found in the Ndk Toolchains Folder for Abi with Prefix: Llvm
Java.Lang.Noclassdeffounderror:Failed Resolution of :Lorg/Apache/Http/Protocolversion
Wrap_Content View Inside a Constraintlayout Stretches Outside the Screen
How to Export Library to Jar in Android Studio
How to Make Custom Dialog with Rounded Corners in Android
Android: Background Image Size (In Pixel) Which Support All Devices