Process分析

本文分析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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值