package com.sunzee.base; import android.app.Application; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.os.Build; import android.os.Bundle; import android.support.multidex.MultiDex; import android.util.Log; import android.view.Display; import android.view.WindowManager; import com.hboxs.serialport.SerialPortDevice; import com.hboxs.serialport.SerialPortManager; import com.orhanobut.hawk.Hawk; import com.sunzee.db.DaoMaster; import com.sunzee.db.DaoSession; import com.sunzee.model.Global; import com.sunzee.model.Heartbeat; import com.sunzee.model.domain.Name; import com.sunzee.service.MyService; import com.sunzee.utils.FileUtil; import com.sunzee.utils.LanguageUtils; import com.sunzee.utils.SharedPreferencesUtils; import com.tencent.bugly.Bugly; import com.tencent.bugly.beta.Beta; import com.umeng.commonsdk.UMConfigure; import com.umeng.commonsdk.UMConfigureImpl; import com.umeng.umcrash.UMCrash; /** * Created by MinKin. */ public class BaseApplication extends Application { public static Context mContext; public static BaseApplication instances; private DaoMaster.DevOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; public static int mRealSizeWidth;//手机屏幕真实宽度 public static int mRealSizeHeight;//手机屏幕真实高度 private static final String TAG = "BaseApplication"; @Override public void onCreate() { super.onCreate(); mContext = this; instances = this; Hawk.init(this).build(); Heartbeat.deviceId = FileUtil.getDeviceId(); Heartbeat.managerId = (String) SharedPreferencesUtils.getParam(Name.SYSTEM_ID, ""); SerialPortDevice device = new SerialPortDevice(Global.dev, "9600"); SerialPortManager.getInstance().open(device); //startMyService(); firstHeartbeat(); setDatabase(); getScreenWidthHeight(); String deviceId = ""; if (Heartbeat.deviceId != null && Heartbeat.deviceId.length() > 7) { deviceId = Heartbeat.deviceId.substring(Heartbeat.deviceId.length() - 6); } else { deviceId = ""; } //友盟注册 UMConfigure.init(this, "5ee80e9fdbc2ec076dd49301", deviceId, UMConfigure.DEVICE_TYPE_PHONE, ""); String ch = (String)SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, "ch"); LanguageUtils.set(ch); // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId // 调试时,将第三个参数改为true //Bugly.init(this,"b764cadec8",true);//有了友盟,这个bugly就不需要了。 } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); //热更新 // you must install multiDex whatever tinker is installed! MultiDex.install(base); // 安装tinker Beta.installTinker(); } //获取屏幕高和宽:不包含虚拟按键部分的高和宽。 public void getScreenWidthHeight() { WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); Point outPoint = new Point(); if (Build.VERSION.SDK_INT >= 19) { // 可能有虚拟按键的情况 display.getRealSize(outPoint); } else { // 不可能有虚拟按键 display.getSize(outPoint); } mRealSizeHeight = outPoint.y; mRealSizeWidth = outPoint.x; Log.d(TAG, "GTgetScreenHeight: " + mRealSizeHeight); Log.d(TAG, "GTgetScreenWidth: " + mRealSizeWidth); } /** * 首次心跳 */ private void firstHeartbeat() { Intent intent = new Intent(this, MyService.class); Bundle bundle = new Bundle(); bundle.putInt("first", 4); intent.putExtras(bundle); startService(intent); } private void startMyService() { Intent intentOne = new Intent(this, MyService.class); startService(intentOne); } public static Context getContext() { return mContext; } //------------------------数据库 public static BaseApplication getInstances() { return instances; } /** * 设置greenDAO */ private void setDatabase() { // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。 // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO已经帮你做了。 // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = mHelper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 mDaoMaster = new DaoMaster(db); mDaoSession = mDaoMaster.newSession(); } public DaoSession getDaoSession() { return mDaoSession; } }