-
Notifications
You must be signed in to change notification settings - Fork 9
/
安卓退出程序方法
90 lines (77 loc) · 1.76 KB
/
安卓退出程序方法
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
public static Context mContext;
private static MyApplication instance;
private static List<Activity> activityList = new LinkedList<Activity>();
@Override
public void onCreate() {
super.onCreate();
mContext = this;
instance = this;
Log.d(TAG, "-----------app init-----------");
// DatabaseAdapter.getDbAdapter();
}
// 添加Activity容器中
public void addActivity(Activity activity) {
activityList.add(activity);
}
// 退出程序
public void exit() {
for (Activity ac : activityList) {
if (ac != null) {
ac.finish();
}
}
System.exit(0);
}
/**
* 获取app的设备上下文
*
* @return
*/
public static Context getContext() {
return mContext;
}
@Override
public void onLowMemory() {
super.onLowMemory();
Log.d(TAG, "-----------onLowMemory-----------");
System.gc();
}
/**
* 判断activity是否在最前端
*
* @param clazz
* @return
*/
public static boolean isTopActivity(String clazz) {
ActivityManager am = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn != null && cn.getClassName().equals(clazz)) {
return true;
}
return false;
}
/**
* 获取实例
*
* @return
*/
public static MyApplication getInstance() {
if (null == instance) {
instance = new MyApplication();
mContext = instance;
return instance;
}
return instance;
}
}