MyApp.java
2.32 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
package com.sunvote.xpadapp;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.sunvote.xpadapp.db.DBLocalHelper;
import com.sunvote.xpadapp.utils.LogUtil;
import com.sunvote.xpadcomm.XPadApi;
import java.util.List;
public class MyApp extends Application {
private static MyApp instance ;
public static XPadApi XPad;
//数据库
private static DBLocalHelper helper;
private static final String TAG = "XpadRD";
private SQLiteDatabase db;
public MyApp(){
instance = this;
}
@Override
public void onCreate() {
super.onCreate();
XPad = XPadApi.getInstance();
//初始化数据库
helper = new DBLocalHelper(this);
db=helper.getWritableDatabase();
LogUtil.enableLogToFile();
final Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
LogUtil.e(TAG,"carsh(thread id:" + thread.getId() + ",thread name:" + thread.getName() + ")");
LogUtil.e(TAG,ex);
if(uncaughtExceptionHandler != null){
uncaughtExceptionHandler.uncaughtException(thread,ex);
}
System.exit(0);
}
});
}
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
// Log.i("��̨", appProcess.processName);
return true;
}else{
// Log.i("ǰ̨", appProcess.processName);
return false;
}
}
}
return false;
}
public static DBLocalHelper getHelper() {
return helper;
}
public static MyApp getInstance() {
return instance;
}
public SQLiteDatabase getDb() {
return db;
}
}