1
2
|
ActivityManager
am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); |
1
|
< uses-permission android:name=\"android.permission.RESTART_PACKAGES\"></ uses-permission > |
1
2
|
android.os.Process.killProcess(android.os.Process.myPid()) //獲取PID System.exit( 0 ); //常規java、c#的標準退出法,返回值爲0代表正常退出 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class MyApplication extends Application
{ //對於新增和刪除操作add和remove,LinedList比較佔優勢,因爲ArrayList實現了基於動態數組的數據結構,要移動數據。LinkedList基於鏈表的數據結構,便於增加刪除 private List<Activity>
activityList = new LinkedList<Activity>(); private static MyApplication
instance; private MyApplication(){
} //單例模式中獲取唯一的MyApplication實例 public static MyApplication
getInstance() { if ( null ==
instance) { instance
= new MyApplication(); } return instance; } //添加Activity到容器中 public void addActivity(Activity
activity) { activityList.add(activity); } //遍歷所有Activity並finish public void exit(){ for (Activity
activity:activityList) { activity.finish(); } System.exit( 0 ); } } |
1
|
MyApplication.getInstance().addActivity( this ); |
1
|
MyApplication.getInstance().exit(); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public abstract class MyAcitivty extends Activity
{ /**負責各個具體
Activity 的顯示**/ public abstract void onCreate(); @Override public void onCreate( final Bundle
savedInstanceState) { //
TODO Auto-generated method stub super .onCreate(savedInstanceState); onCreate(); RegListener(); } /**註冊退出事件監聽**/ public void RegListener()
{ ExitListenerReceiver
exitre = new ExitListenerReceiver(); IntentFilter
intentfilter = new IntentFilter(); intentfilter.addAction( this .getPackageName()
+ "." + "ExitListenerReceiver" ); this .registerReceiver(exitre,
intentfilter); } class ExitListenerReceiver extends BroadcastReceiver
{ @Override public void onReceive(Context
context, Intent i) { ((Activity)
context).finish(); } } } |
1
2
|
Intent
intent = new Intent(context.getPackageName()+ ".ExitListenerReceiver" ); context.sendBroadcast(intent); |
1
|
public boolean static isCloseAll= false ; |
1
2
3
4
5
|
@Override onResume()
{ super .onResume(); if (Setting.isCloseAll)
finish(); } |
1
2
|
Setting.isCloseAll= true ; finish(); |
1
2
3
4
5
6
7
8
9
10
|
package com.example.android_uncatchexception; import android.app.Application; public class MyCrashApplication extends Application
{ @Override public void onCreate()
{ super .onCreate(); //程序一啓動,就將未捕獲異常初始化 CrashHandler.getInstance().init(getApplicationContext()); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.example.android_uncatchexception; import java.lang.Thread.UncaughtExceptionHandler; import android.content.Context; import android.util.Log; /** *
自定義異常捕獲類 * *
@author ZHF */ public class CrashHandler implements UncaughtExceptionHandler
{ public static final String TAG
= "CrashHandler" ; //
程序的Context對象 private Context
mContext; /**
單例模式 **/ private CrashHandler()
{ } /**
懶漢式 **/ private static class CrashHolder
{ static final CrashHandler
crashHandler = new CrashHandler(); } public static CrashHandler
getInstance() { return CrashHolder.crashHandler; } public void init(Context
context) { mContext
= context; //
設置該CrashHandler爲程序的默認處理器 Thread.setDefaultUncaughtExceptionHandler( this ); } @Override public void uncaughtException(Thread
thread, Throwable ex) { Log.d(TAG, "uncaughtException--->thread" +
thread + "
name: " +
thread.getName() + "
id: " +
thread.getId() + "exception--->" +
ex); String threadName
= thread.getName(); if ( "main" .equals(threadName))
{ Log.d(TAG, "在主線程的崩潰!" ); } else { //這裏我們根據thread
name來進行區別對待:可以將異常信息寫入文件供以後分析 Log.d(TAG, "在子線程中崩潰!" ); } android.os.Process.killProcess(android.os.Process.myPid()); //殺死該進程 System.exit( 0 ); //退出 } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.example.android_uncatchexception; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity
{ Button
mBtn; String str; //不要初始化,爲了下面製造空指針異常 @Override protected void onCreate(Bundle
savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtn
= (Button) this .findViewById(R.id.button1); mBtn.setOnClickListener( new OnClickListener()
{ @Override public void onClick(View
v) { //NullPointerException System.out.println(str); } }); } } |