MyApplication.java 11 KB

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