MyApp.java 2.32 KB
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;
	}
}