MyApplication.java 8.9 KB

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