4

I want to force-stop an application from my Android app, (Instead of doing manually like apps-force->stop). How to achieve this?

I used:

android.os.Process.killProcess(android.os.Process.myPid());
system.exit(0);

It crashes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
R KiranKumar
  • 825
  • 1
  • 8
  • 27

3 Answers3

3

android.os.Process.killProcess(android.os.Process.myPid()); this code is correct and best one. If you need more information Refer link as How to close Android application? and Process.

You can also try this code

Intent intent = new Intent(yourCurrentActivity.this, yourNextActivity.class);  
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);  
android.os.Process.killProcess(android.os.Process.myPid());

You start an activity, and then close current activity.

Community
  • 1
  • 1
Sekar
  • 1,061
  • 7
  • 21
  • 38
  • where are you using this code `android.os.Process.killProcess(android.os.Process.myPid());`. Are you want to close your application or 3rd party application. – Sekar Aug 30 '12 at 12:06
  • on last activity , i have give this inside onclick() – R KiranKumar Aug 30 '12 at 12:11
  • this code for start home activity , i want to force stop my app – R KiranKumar Aug 30 '12 at 12:52
  • This code is working for me. Please check.. You start another activity and close current activity. – Sekar Aug 31 '12 at 06:53
  • I don't want to start a new activity. All I need is to close an application like the manual one do(go to apps-> forcestop). I want it to be done programatically. – R KiranKumar Aug 31 '12 at 11:05
2

Just put this code in your close button and your app will stop

private void endapp() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid());
}

You can try with this one also

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
Mahesh
  • 1,559
  • 6
  • 27
  • 57
-1

try this...

private void QuitApp() {

    //getActivity().moveTaskToBack(true); //Move the task containing this activity to the back of the activity stack. 

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
  }
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
  • This is fundamentally misguided - it is not the job of the app developer to terminate the process, rather they should simply call finish() if its work is concluded. The system will decide the appropriate process lifetime. – Chris Stratton Apr 22 '15 at 06:00