MyApplication.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package com.xzjmyk.pm.activity;
  2. import android.app.Application;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Bitmap.Config;
  5. import android.os.Build;
  6. import android.os.Environment;
  7. import android.os.Handler;
  8. import android.os.StrictMode;
  9. import android.util.Log;
  10. import com.baidu.mapapi.SDKInitializer;
  11. import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
  12. import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
  13. import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
  14. import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
  15. import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
  16. import com.nostra13.universalimageloader.core.DisplayImageOptions;
  17. import com.nostra13.universalimageloader.core.ImageLoader;
  18. import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  19. import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
  20. import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
  21. import com.xzjmyk.pm.activity.bean.ConfigBean;
  22. import com.xzjmyk.pm.activity.bean.User;
  23. import com.xzjmyk.pm.activity.db.SQLiteHelper;
  24. import com.xzjmyk.pm.activity.util.PreferenceUtils;
  25. import com.xzjmyk.pm.activity.util.Constants;
  26. import com.xzjmyk.pm.activity.volley.FastVolley;
  27. import org.apache.http.cookie.Cookie;
  28. import java.io.File;
  29. public class MyApplication extends Application {
  30. /**@注释:记录Cookie */
  31. public String JSESSION_B2B;
  32. public static Cookie cookie;
  33. private static MyApplication INSTANCE = null;
  34. public static MyApplication getInstance() {
  35. return INSTANCE;
  36. }
  37. @Override
  38. public void onCreate() {
  39. super.onCreate();
  40. INSTANCE = this;
  41. PreferenceUtils.putBoolean(this, Constants.IS_NOTIFICATION, false);//不进行通知
  42. new Handler().postDelayed(new Runnable() {
  43. @Override
  44. public void run() {
  45. SDKInitializer.initialize(getApplicationContext());
  46. // 初始化数据库
  47. SQLiteHelper.copyDatabaseFile(getApplicationContext());
  48. // 初始化定位
  49. getBdLocationHelper();
  50. // 初始化App目录
  51. initAppDir();
  52. // 初始化图片加载
  53. initImageLoader();
  54. }
  55. }, 1);
  56. if (AppConfig.DEBUG) {
  57. Log.d(AppConfig.TAG, "MyApplication onCreate");
  58. }
  59. if (AppConfig.DEBUG) {
  60. StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
  61. StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
  62. }
  63. // 初始化网络监听
  64. mNetWorkObservable = new NetWorkObservable(this);
  65. }
  66. /**
  67. * 在程序内部关闭时,调用此方法
  68. */
  69. public void destory() {
  70. if (AppConfig.DEBUG) {
  71. Log.d(AppConfig.TAG, "MyApplication destory");
  72. }
  73. // 结束百度定位
  74. if (mBdLocationHelper != null) {
  75. mBdLocationHelper.release();
  76. }
  77. // 关闭网络状态的监听
  78. if (mNetWorkObservable != null) {
  79. mNetWorkObservable.release();
  80. }
  81. // 清除图片加载
  82. ImageLoader.getInstance().destroy();
  83. //
  84. releaseFastVolley();
  85. // 释放数据库
  86. // SQLiteHelper.release();
  87. android.os.Process.killProcess(android.os.Process.myPid());
  88. }
  89. /********************* 百度地图定位服务 ************************/
  90. private BdLocationHelper mBdLocationHelper;
  91. public BdLocationHelper getBdLocationHelper() {
  92. if (mBdLocationHelper == null) {
  93. mBdLocationHelper = new BdLocationHelper(this);
  94. }
  95. return mBdLocationHelper;
  96. }
  97. /********************* 提供网络全局监听 ************************/
  98. private NetWorkObservable mNetWorkObservable;
  99. public boolean isNetworkActive() {
  100. if (mNetWorkObservable != null) {
  101. return mNetWorkObservable.isNetworkActive();
  102. }
  103. return true;
  104. }
  105. public void registerNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
  106. if (mNetWorkObservable != null) {
  107. mNetWorkObservable.registerObserver(observer);
  108. }
  109. }
  110. public void unregisterNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
  111. if (mNetWorkObservable != null) {
  112. mNetWorkObservable.unregisterObserver(observer);
  113. }
  114. }
  115. /* 文件缓存的目录 */
  116. public String mAppDir;
  117. public String mPicturesDir;
  118. public String mVoicesDir;
  119. public String mVideosDir;
  120. public String mFilesDir;
  121. private void initAppDir() {
  122. File file = getExternalFilesDir(null);
  123. if(file!=null) {
  124. if (!file.exists()) {
  125. file.mkdirs();
  126. }
  127. mAppDir = file.getAbsolutePath();
  128. }
  129. file = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  130. if(file!=null) {
  131. if (!file.exists()) {
  132. file.mkdirs();
  133. }
  134. mPicturesDir = file.getAbsolutePath();
  135. }
  136. file = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
  137. if(file!=null) {
  138. if (!file.exists()) {
  139. file.mkdirs();
  140. }
  141. mVoicesDir = file.getAbsolutePath();
  142. }
  143. file = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
  144. if(file!=null) {
  145. if (!file.exists()) {
  146. file.mkdirs();
  147. }
  148. mVideosDir = file.getAbsolutePath();
  149. }
  150. file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
  151. if(file!=null) {
  152. if (!file.exists()) {
  153. file.mkdirs();
  154. }
  155. mFilesDir = file.getAbsolutePath();
  156. }
  157. }
  158. /******************* 初始化图片加载 **********************/
  159. // 显示的设置
  160. public static DisplayImageOptions mNormalImageOptions;
  161. public static DisplayImageOptions mAvatarRoundImageOptions;
  162. public static DisplayImageOptions mAvatarNormalImageOptions;
  163. private void initImageLoader() {
  164. int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 5);
  165. MemoryCacheAware<String, Bitmap> memoryCache;
  166. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
  167. memoryCache = new LruMemoryCache(memoryCacheSize);
  168. } else {
  169. memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
  170. }
  171. mNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  172. .resetViewBeforeLoading(false).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon)
  173. .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon).build();
  174. mAvatarRoundImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  175. .displayer(new RoundedBitmapDisplayer(10)).resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
  176. .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
  177. mAvatarNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  178. .resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
  179. .showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
  180. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(mNormalImageOptions)
  181. // .denyCacheImageMultipleSizesInMemory()
  182. .discCache(new TotalSizeLimitedDiscCache(new File(mPicturesDir), 50 * 1024 * 1024))
  183. // 最多缓存50M的图片
  184. .discCacheFileNameGenerator(new Md5FileNameGenerator()).memoryCache(memoryCache).tasksProcessingOrder(QueueProcessingType.LIFO)
  185. .threadPriority(Thread.NORM_PRIORITY - 2).threadPoolSize(4).build();
  186. // Initialize ImageLoader with configuration.
  187. ImageLoader.getInstance().init(config);
  188. }
  189. /********************* 提供全局配置 ************************/
  190. private AppConfig mConfig;
  191. public void setConfig(AppConfig config) {
  192. mConfig = config;
  193. }
  194. public AppConfig getConfig() {
  195. if (mConfig == null) {
  196. mConfig = AppConfig.initConfig(getApplicationContext(), new ConfigBean());
  197. }
  198. return mConfig;
  199. }
  200. /***************** 提供全局的Volley ***************************/
  201. private FastVolley mFastVolley;
  202. public FastVolley getFastVolley() {
  203. if (mFastVolley == null) {
  204. synchronized (MyApplication.class) {
  205. if (mFastVolley == null) {
  206. mFastVolley = new FastVolley(this);
  207. mFastVolley.start();
  208. }
  209. }
  210. }
  211. return mFastVolley;
  212. }
  213. private void releaseFastVolley() {
  214. if (mFastVolley != null) {
  215. mFastVolley.stop();
  216. }
  217. }
  218. /*********************** 保存当前登陆用户的全局信息 ***************/
  219. public String roomName;
  220. public String mAccessToken;
  221. public long mExpiresIn;
  222. public int mUserStatus;
  223. public boolean mUserStatusChecked = false;
  224. public User mLoginUser = new User();// 当前登陆的用户
  225. /*********************** 保存其他用户坐标信息 ***************/
  226. public String getJSESSION_B2B() {
  227. return JSESSION_B2B;
  228. }
  229. public void setJSESSION_B2B(String jSESSION_B2B) {
  230. JSESSION_B2B = jSESSION_B2B;
  231. }
  232. public static Cookie getCookie() {
  233. return cookie;
  234. }
  235. public static void setCookie(Cookie cookie) {
  236. MyApplication.cookie = cookie;
  237. }
  238. }