MyApplication.java 11 KB

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