2

What is the difference between

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

and

System.exit(1);

I know that both kill the process. but, I have felt the difference that on killing a process, when it is restarted, it is started from a previous state. But, I want to know the technical details behind such error.

ajduke
  • 4,991
  • 7
  • 36
  • 56
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59

2 Answers2

2

Technically, killing a process like this sends the process a SIG_TERM and lets it shutdowm somewhat gracefully. System.exit(1) will just exit the JVM. You could also send a -9 = SIG_KILL signal to the process using

http://developer.android.com/reference/android/os/Process.html#sendSignal(int, int)

And this would kill the process immediately. I don't recommend any of these, and suggest using finish(). The only exception I can think of might be if you are doing something that is spawning lots of child processes and you want to shut them down at some point.

hack_on
  • 2,532
  • 4
  • 26
  • 30
1
System.exit(1);

Usually a non-zero error status indicates that the program ended abnormally and

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

Process is Tools for managing OS processes.

android.os.Process.killProcess(id);

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93