BaseApplication.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.sunzee.base;
  2. import android.app.Application;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.Point;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.Display;
  11. import android.view.WindowManager;
  12. import com.hboxs.serialport.SerialPortDevice;
  13. import com.hboxs.serialport.SerialPortManager;
  14. import com.orhanobut.hawk.Hawk;
  15. import com.sunzee.db.DaoMaster;
  16. import com.sunzee.db.DaoSession;
  17. import com.sunzee.model.Heartbeat;
  18. import com.sunzee.model.domain.Name;
  19. import com.sunzee.service.MyService;
  20. import com.sunzee.utils.FileUtil;
  21. import com.sunzee.utils.SharedPreferencesUtils;
  22. /**
  23. * Created by MinKin.
  24. */
  25. public class BaseApplication extends Application {
  26. public static Context mContext;
  27. public static BaseApplication instances;
  28. private DaoMaster.DevOpenHelper mHelper;
  29. private SQLiteDatabase db;
  30. private DaoMaster mDaoMaster;
  31. private DaoSession mDaoSession;
  32. public static int mRealSizeWidth;//手机屏幕真实宽度
  33. public static int mRealSizeHeight;//手机屏幕真实高度
  34. private static final String TAG = "BaseApplication";
  35. @Override
  36. public void onCreate() {
  37. super.onCreate();
  38. mContext = this;
  39. instances = this;
  40. Hawk.init(this).build();
  41. Heartbeat.deviceId = FileUtil.getDeviceId();
  42. Heartbeat.managerId = (String) SharedPreferencesUtils.getParam(Name.SYSTEM_ID, "");
  43. SerialPortDevice device = new SerialPortDevice("/dev/ttyS2", "9600");
  44. SerialPortManager.getInstance().open(device);
  45. startMyService();
  46. firstHeartbeat();
  47. setDatabase();
  48. getScreenWidthHeight();
  49. }
  50. //获取屏幕高和宽:不包含虚拟按键部分的高和宽。
  51. public void getScreenWidthHeight() {
  52. WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  53. Display display = windowManager.getDefaultDisplay();
  54. Point outPoint = new Point();
  55. if (Build.VERSION.SDK_INT >= 19) {
  56. // 可能有虚拟按键的情况
  57. display.getRealSize(outPoint);
  58. } else {
  59. // 不可能有虚拟按键
  60. display.getSize(outPoint);
  61. }
  62. mRealSizeHeight = outPoint.y;
  63. mRealSizeWidth = outPoint.x;
  64. Log.d(TAG, "GTgetScreenHeight: " + mRealSizeHeight);
  65. Log.d(TAG, "GTgetScreenWidth: " + mRealSizeWidth);
  66. }
  67. /**
  68. * 首次心跳
  69. */
  70. private void firstHeartbeat() {
  71. Intent intent = new Intent(this, MyService.class);
  72. Bundle bundle = new Bundle();
  73. bundle.putInt("first", 4);
  74. intent.putExtras(bundle);
  75. startService(intent);
  76. }
  77. private void startMyService() {
  78. Intent intentOne = new Intent(this, MyService.class);
  79. startService(intentOne);
  80. }
  81. public static Context getContext() {
  82. return mContext;
  83. }
  84. //------------------------数据库
  85. public static BaseApplication getInstances() {
  86. return instances;
  87. }
  88. /**
  89. * 设置greenDAO
  90. */
  91. private void setDatabase() {
  92. // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。
  93. // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO已经帮你做了。
  94. // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
  95. // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
  96. mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
  97. db = mHelper.getWritableDatabase();
  98. // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
  99. mDaoMaster = new DaoMaster(db);
  100. mDaoSession = mDaoMaster.newSession();
  101. }
  102. public DaoSession getDaoSession() {
  103. return mDaoSession;
  104. }
  105. }