BaseApplication.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.support.multidex.MultiDex;
  10. import android.util.Log;
  11. import android.view.Display;
  12. import android.view.WindowManager;
  13. import com.hboxs.serialport.SerialPortDevice;
  14. import com.hboxs.serialport.SerialPortManager;
  15. import com.orhanobut.hawk.Hawk;
  16. import com.sunzee.db.DaoMaster;
  17. import com.sunzee.db.DaoSession;
  18. import com.sunzee.model.Global;
  19. import com.sunzee.model.Heartbeat;
  20. import com.sunzee.model.domain.Name;
  21. import com.sunzee.service.MyService;
  22. import com.sunzee.utils.FileUtil;
  23. import com.sunzee.utils.LanguageUtils;
  24. import com.sunzee.utils.SharedPreferencesUtils;
  25. import com.tencent.bugly.Bugly;
  26. import com.tencent.bugly.beta.Beta;
  27. import com.umeng.commonsdk.UMConfigure;
  28. import com.umeng.commonsdk.UMConfigureImpl;
  29. import com.umeng.umcrash.UMCrash;
  30. /**
  31. * Created by MinKin.
  32. */
  33. public class BaseApplication extends Application {
  34. public static Context mContext;
  35. public static BaseApplication instances;
  36. private DaoMaster.DevOpenHelper mHelper;
  37. private SQLiteDatabase db;
  38. private DaoMaster mDaoMaster;
  39. private DaoSession mDaoSession;
  40. public static int mRealSizeWidth;//手机屏幕真实宽度
  41. public static int mRealSizeHeight;//手机屏幕真实高度
  42. private static final String TAG = "BaseApplication";
  43. @Override
  44. public void onCreate() {
  45. super.onCreate();
  46. mContext = this;
  47. instances = this;
  48. Hawk.init(this).build();
  49. Heartbeat.deviceId = FileUtil.getDeviceId();
  50. Heartbeat.managerId = (String) SharedPreferencesUtils.getParam(Name.SYSTEM_ID, "");
  51. SerialPortDevice device = new SerialPortDevice(Global.dev, "9600");
  52. SerialPortManager.getInstance().open(device);
  53. //startMyService();
  54. firstHeartbeat();
  55. setDatabase();
  56. getScreenWidthHeight();
  57. String deviceId = "";
  58. if (Heartbeat.deviceId != null && Heartbeat.deviceId.length() > 7) {
  59. deviceId = Heartbeat.deviceId.substring(Heartbeat.deviceId.length() - 6);
  60. } else {
  61. deviceId = "";
  62. }
  63. //友盟注册
  64. UMConfigure.init(this, "5ee80e9fdbc2ec076dd49301", deviceId, UMConfigure.DEVICE_TYPE_PHONE, "");
  65. String ch = (String)SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, "ch");
  66. LanguageUtils.set(ch);
  67. // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
  68. // 调试时,将第三个参数改为true
  69. //Bugly.init(this,"b764cadec8",true);//有了友盟,这个bugly就不需要了。
  70. }
  71. @Override
  72. protected void attachBaseContext(Context base) {
  73. super.attachBaseContext(base);
  74. //热更新
  75. // you must install multiDex whatever tinker is installed!
  76. MultiDex.install(base);
  77. // 安装tinker
  78. Beta.installTinker();
  79. }
  80. //获取屏幕高和宽:不包含虚拟按键部分的高和宽。
  81. public void getScreenWidthHeight() {
  82. WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  83. Display display = windowManager.getDefaultDisplay();
  84. Point outPoint = new Point();
  85. if (Build.VERSION.SDK_INT >= 19) {
  86. // 可能有虚拟按键的情况
  87. display.getRealSize(outPoint);
  88. } else {
  89. // 不可能有虚拟按键
  90. display.getSize(outPoint);
  91. }
  92. mRealSizeHeight = outPoint.y;
  93. mRealSizeWidth = outPoint.x;
  94. Log.d(TAG, "GTgetScreenHeight: " + mRealSizeHeight);
  95. Log.d(TAG, "GTgetScreenWidth: " + mRealSizeWidth);
  96. }
  97. /**
  98. * 首次心跳
  99. */
  100. private void firstHeartbeat() {
  101. Intent intent = new Intent(this, MyService.class);
  102. Bundle bundle = new Bundle();
  103. bundle.putInt("first", 4);
  104. intent.putExtras(bundle);
  105. startService(intent);
  106. }
  107. private void startMyService() {
  108. Intent intentOne = new Intent(this, MyService.class);
  109. startService(intentOne);
  110. }
  111. public static Context getContext() {
  112. return mContext;
  113. }
  114. //------------------------数据库
  115. public static BaseApplication getInstances() {
  116. return instances;
  117. }
  118. /**
  119. * 设置greenDAO
  120. */
  121. private void setDatabase() {
  122. // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。
  123. // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO已经帮你做了。
  124. // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
  125. // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
  126. mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
  127. db = mHelper.getWritableDatabase();
  128. // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
  129. mDaoMaster = new DaoMaster(db);
  130. mDaoSession = mDaoMaster.newSession();
  131. }
  132. public DaoSession getDaoSession() {
  133. return mDaoSession;
  134. }
  135. }