| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package com.xzjmyk.pm.activity;
- import android.app.Application;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.os.Build;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.StrictMode;
- import android.util.Log;
- import com.baidu.mapapi.SDKInitializer;
- import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
- import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
- import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
- import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
- import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
- import com.nostra13.universalimageloader.core.DisplayImageOptions;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
- import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
- import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
- import com.xzjmyk.pm.activity.bean.ConfigBean;
- import com.xzjmyk.pm.activity.bean.User;
- import com.xzjmyk.pm.activity.db.SQLiteHelper;
- import com.xzjmyk.pm.activity.util.PreferenceUtils;
- import com.xzjmyk.pm.activity.util.Constants;
- import com.xzjmyk.pm.activity.volley.FastVolley;
- import org.apache.http.cookie.Cookie;
- import java.io.File;
- public class MyApplication extends Application {
-
- /**@注释:记录Cookie */
- public String JSESSION_B2B;
- public static Cookie cookie;
-
-
- private static MyApplication INSTANCE = null;
- public static MyApplication getInstance() {
- return INSTANCE;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- INSTANCE = this;
- PreferenceUtils.putBoolean(this, Constants.IS_NOTIFICATION, false);//不进行通知
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- SDKInitializer.initialize(getApplicationContext());
- // 初始化数据库
- SQLiteHelper.copyDatabaseFile(getApplicationContext());
- // 初始化定位
- getBdLocationHelper();
- // 初始化App目录
- initAppDir();
- // 初始化图片加载
- initImageLoader();
- }
- }, 1);
- if (AppConfig.DEBUG) {
- Log.d(AppConfig.TAG, "MyApplication onCreate");
- }
- if (AppConfig.DEBUG) {
- StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
- StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
- }
- // 初始化网络监听
- mNetWorkObservable = new NetWorkObservable(this);
- }
- /**
- * 在程序内部关闭时,调用此方法
- */
- public void destory() {
- if (AppConfig.DEBUG) {
- Log.d(AppConfig.TAG, "MyApplication destory");
- }
- // 结束百度定位
- if (mBdLocationHelper != null) {
- mBdLocationHelper.release();
- }
- // 关闭网络状态的监听
- if (mNetWorkObservable != null) {
- mNetWorkObservable.release();
- }
- // 清除图片加载
- ImageLoader.getInstance().destroy();
- //
- releaseFastVolley();
- // 释放数据库
- // SQLiteHelper.release();
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- /********************* 百度地图定位服务 ************************/
- private BdLocationHelper mBdLocationHelper;
- public BdLocationHelper getBdLocationHelper() {
- if (mBdLocationHelper == null) {
- mBdLocationHelper = new BdLocationHelper(this);
- }
- return mBdLocationHelper;
- }
- /********************* 提供网络全局监听 ************************/
- private NetWorkObservable mNetWorkObservable;
- public boolean isNetworkActive() {
- if (mNetWorkObservable != null) {
- return mNetWorkObservable.isNetworkActive();
- }
- return true;
- }
- public void registerNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
- if (mNetWorkObservable != null) {
- mNetWorkObservable.registerObserver(observer);
- }
- }
- public void unregisterNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
- if (mNetWorkObservable != null) {
- mNetWorkObservable.unregisterObserver(observer);
- }
- }
- /* 文件缓存的目录 */
- public String mAppDir;
- public String mPicturesDir;
- public String mVoicesDir;
- public String mVideosDir;
- public String mFilesDir;
- private void initAppDir() {
- File file = getExternalFilesDir(null);
- if(file!=null) {
- if (!file.exists()) {
- file.mkdirs();
- }
- mAppDir = file.getAbsolutePath();
- }
- file = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
- if(file!=null) {
- if (!file.exists()) {
- file.mkdirs();
- }
- mPicturesDir = file.getAbsolutePath();
- }
- file = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
- if(file!=null) {
- if (!file.exists()) {
- file.mkdirs();
- }
- mVoicesDir = file.getAbsolutePath();
- }
- file = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
- if(file!=null) {
- if (!file.exists()) {
- file.mkdirs();
- }
- mVideosDir = file.getAbsolutePath();
- }
- file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
- if(file!=null) {
- if (!file.exists()) {
- file.mkdirs();
- }
- mFilesDir = file.getAbsolutePath();
- }
- }
- /******************* 初始化图片加载 **********************/
- // 显示的设置
- public static DisplayImageOptions mNormalImageOptions;
- public static DisplayImageOptions mAvatarRoundImageOptions;
- public static DisplayImageOptions mAvatarNormalImageOptions;
- private void initImageLoader() {
- int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 5);
- MemoryCacheAware<String, Bitmap> memoryCache;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
- memoryCache = new LruMemoryCache(memoryCacheSize);
- } else {
- memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
- }
- mNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
- .resetViewBeforeLoading(false).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon)
- .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon).build();
- mAvatarRoundImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
- .displayer(new RoundedBitmapDisplayer(10)).resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
- .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
- mAvatarNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
- .resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
- .showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(mNormalImageOptions)
- // .denyCacheImageMultipleSizesInMemory()
- .discCache(new TotalSizeLimitedDiscCache(new File(mPicturesDir), 50 * 1024 * 1024))
- // 最多缓存50M的图片
- .discCacheFileNameGenerator(new Md5FileNameGenerator()).memoryCache(memoryCache).tasksProcessingOrder(QueueProcessingType.LIFO)
- .threadPriority(Thread.NORM_PRIORITY - 2).threadPoolSize(4).build();
- // Initialize ImageLoader with configuration.
- ImageLoader.getInstance().init(config);
- }
- /********************* 提供全局配置 ************************/
- private AppConfig mConfig;
- public void setConfig(AppConfig config) {
- mConfig = config;
- }
- public AppConfig getConfig() {
- if (mConfig == null) {
- mConfig = AppConfig.initConfig(getApplicationContext(), new ConfigBean());
- }
- return mConfig;
- }
- /***************** 提供全局的Volley ***************************/
- private FastVolley mFastVolley;
- public FastVolley getFastVolley() {
- if (mFastVolley == null) {
- synchronized (MyApplication.class) {
- if (mFastVolley == null) {
- mFastVolley = new FastVolley(this);
- mFastVolley.start();
- }
- }
- }
- return mFastVolley;
- }
- private void releaseFastVolley() {
- if (mFastVolley != null) {
- mFastVolley.stop();
- }
- }
- /*********************** 保存当前登陆用户的全局信息 ***************/
- public String roomName;
- public String mAccessToken;
- public long mExpiresIn;
- public int mUserStatus;
- public boolean mUserStatusChecked = false;
- public User mLoginUser = new User();// 当前登陆的用户
-
- /*********************** 保存其他用户坐标信息 ***************/
- public String getJSESSION_B2B() {
- return JSESSION_B2B;
- }
- public void setJSESSION_B2B(String jSESSION_B2B) {
- JSESSION_B2B = jSESSION_B2B;
- }
- public static Cookie getCookie() {
- return cookie;
- }
- public static void setCookie(Cookie cookie) {
- MyApplication.cookie = cookie;
- }
- }
|