本文分析android.os.Process
1、Android杀死APP。示例演示。
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_kill:
//不调用onDestroy,杀死Application
Process.killProcess(Process.myPid());
break;
case R.id.btn_exit:
//ensure that, when VM is about to exit, all objects are finalized
System.runFinalizersOnExit(true);
//不调用onDestroy,杀死Application
//VM stop,program exit
System.exit(0);
break;
case R.id.btn_finish:
//调用onDestroy,不会杀死Application
this.finish();
break;
case R.id.btn_thread:
Thread t = Thread.currentThread();
//查看进程信息
Log.d(TAG, t.getName()+"-"+t.getId()+"-"+t.getPriority()
+"-"+t.getState().name()+"-"+t.getThreadGroup().getName());
//设置线程优先级,Libcore.os.gettid()
Process.setThreadPriority(Process.myTid(), 1);
Log.d(TAG, Process.myTid()+"-priority:"+Process.getThreadPriority(Process.myTid()));
//VMThread.currentThread()
t.setPriority(1);
Log.d(TAG, t.getId()+"-priority:"+t.getPriority());
break;
case R.id.btn_second:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_kill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Process.killProcess"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="System.exit"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity.finish"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_thread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thread.stop"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="second"
android:onClick="onClick"/>
</LinearLayout>
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.w(getClass().getSimpleName(), "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
protected void onDestroy() {
Log.w(getClass().getSimpleName(), "onDestroy");
super.onDestroy();
}
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_kill:
Process.killProcess(Process.myPid());
break;
case R.id.btn_exit:
System.exit(0);
break;
case R.id.btn_third:
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_kill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Process.killProcess"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="System.exit"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="third"
android:onClick="onClick"/>
</LinearLayout>
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.w(getClass().getSimpleName(), "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
@Override
protected void onDestroy() {
Log.w(getClass().getSimpleName(), "onDestroy");
super.onDestroy();
}
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_kill:
//杀死APP,然后直接创建SecondActivity,摁back键创建MainActivity销毁SecondActivity
Process.killProcess(Process.myPid());
break;
case R.id.btn_exit:
System.exit(0);
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_kill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Process.killProcess"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="System.exit"
android:onClick="onClick"/>
</LinearLayout>