123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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.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.Heartbeat;
- import com.sunzee.model.domain.Name;
- import com.sunzee.service.MyService;
- import com.sunzee.utils.FileUtil;
- import com.sunzee.utils.SharedPreferencesUtils;
- /**
- * 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("/dev/ttyS2", "9600");
- SerialPortManager.getInstance().open(device);
- startMyService();
- firstHeartbeat();
- setDatabase();
- getScreenWidthHeight();
- }
- //获取屏幕高和宽:不包含虚拟按键部分的高和宽。
- 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;
- }
- }
|