Process分析
来源:互联网 发布:袁隆平 诺贝尔奖 知乎 编辑:程序博客网 时间:2024/04/30 04:04
本文分析android.os.Process
1、Android杀死APP。示例演示。
public class MainActivity extends Activity {private static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {Log.d(TAG, "onCreate");super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overrideprotected void onDestroy() {Log.d(TAG, "onDestroy");super.onDestroy();}public void onClick(View v){switch (v.getId()) {case R.id.btn_kill://不调用onDestroy,杀死ApplicationProcess.killProcess(Process.myPid());break;case R.id.btn_exit://ensure that, when VM is about to exit, all objects are finalizedSystem.runFinalizersOnExit(true);//不调用onDestroy,杀死Application//VM stop,program exitSystem.exit(0);break;case R.id.btn_finish://调用onDestroy,不会杀死Applicationthis.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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {Log.w(getClass().getSimpleName(), "onCreate");super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);}@Overrideprotected 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {Log.w(getClass().getSimpleName(), "onCreate");super.onCreate(savedInstanceState);setContentView(R.layout.activity_third);}@Overrideprotected 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销毁SecondActivityProcess.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
- Process分析
- Surfaceflinger process流程分析
- Surfaceflinger process流程分析
- Surfaceflinger process流程分析
- process流阻塞分析
- Process crashed问题分析
- Android SurfaceFlinger process 流程分析
- [Java][Android][Process] Process 创建+控制+分析 经验浅谈
- Linux Boot Process 分析与研究
- process XXX has died的log分析
- LTE Femto UL Harq Process Number 分析
- Android Surfac eFlinger process 流程分析
- ActivityManagerService启动新的process过程分析
- nginx -cache loader process 进程分析
- nginx -cache loader process进程分析
- Nginx-cache manager process进程分析
- CFD分析过程(CFD Analysis Process)
- NVME Drive分析之 Detailed IO Process
- A and B and Compilation Errors——找出消失的数字
- 黑马程序员——GUI(3)菜单&FileDialog
- 【android仿系列进阶篇】android 支付宝手机网页支付
- 节后轻生折射老人“空巢”之痛
- error LNK2001: 无法解析的外部符号 __imp__RegEnumKeyExA@32
- Process分析
- R语言入门(一)
- eclipse+maven+ssh
- DoubleListView效果
- 女儿的幸福来自妈妈的教育
- vs2010 OpenGL 环境
- error LNK2019: 无法解析的外部符号 __imp__GetStockObject@4该符号在函数_WinMain@16 中被引用
- v7 actionbar 没有显示 overflow的问题
- 前端切图,把psd文件做成一个网页