美文网首页
关闭 SDK 中的Activity ActivityLifecy

关闭 SDK 中的Activity ActivityLifecy

作者: 钉子石 | 来源:发表于2017-06-08 11:19 被阅读0次

    项目在开发的时候 有单点登录的功能要做
    项目中有 支付宝 微信充值 主要是 支付宝充值 如手机中没有支付宝应用 就在应用内打开h5的支付宝充值页面 这就涉及到 第三方sdk 中的activity关闭问题

    注册 ActivityLifecycleCallbacks 监听应用内所有 Activity的生命周期

    public class GApplication extends Application {
        //记录当前栈里所有activity
        private List<Activity> activities = new ArrayList<Activity>();
        //记录需要一次性关闭的页面
        private List<Activity> activitys = new ArrayList<Activity>();
    
        private static GApplication instance;
    
        DataBaseHelper dbHelper;
        private SharedPreferencesUtil sharedPreferencesUtil;
        //声明AMapLocationClient类对象
        public ApiLogin apiLoginUserInfo;
        public boolean openLogin = false;
        public boolean pushIsForce = false;
        public boolean isEditOrder = false;
    
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    
        /**
         * 获得实例
         *
         * @return
         */
        public static GApplication getInstance() {
            return instance;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this, "fonts/robotoregular.ttf");
            registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
            instance = this;
            //禁止默认的页面统计方式
            MobclickAgent.openActivityDurationTrack(false);
            //debug模式
            MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);
            CrashReport.initCrashReport(getApplicationContext(), "73b6bbb172", true);
            sharedPreferencesUtil = SharedPreferencesUtil.getInstance(this);
            sanxinBug();
            initUser();
        }
    
        public SharedPreferencesUtil getSharedPreferencesUtil() {
            return sharedPreferencesUtil;
        }
    
        public DataBaseHelper getDbHelper() {
            return dbHelper;
        }
    
        public void initUser() {
            apiLoginUserInfo = new ApiLogin();
            if (apiLoginUserInfo == null) {
                ApiLogin apiLogin = JacksonUtil.getInstance().readValue(sharedPreferencesUtil.getGsonUserInfo(), ApiLogin.class);
                if (apiLogin != null)
                    apiLoginUserInfo = apiLogin;
            }
        }
    
        //三星键盘系统bug
        public void sanxinBug() {
            try {
                Class cls = Class.forName("android.sec.clipboard.ClipboardUIManager");
                Method m = cls.getDeclaredMethod("getInstance", Context.class);
                m.setAccessible(true);
                m.invoke(null, this);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 新建了一个activity
         *
         * @param activity
         */
        public void addActivity(Activity activity) {
            activities.add(activity);
        }
    
        /**
         * 结束指定的Activity
         *
         * @param activity
         */
        public void finishActivity(Activity activity) {
            if (activity != null) {
                this.activities.remove(activity);
                activity.finish();
                activity = null;
            }
        }
     /**
         * 应用退出,结束除了main所有的activity
         */
        public void exitNoMain() {
            for (Activity activity : activities) {
                if (activity != null) {
                    if (activity instanceof MainActivity) {
    
                    } else {
                        activity.finish();
                    }
                }
            }
        }
    
        /**
         * 应用退出,结束所有的activity
         */
        public void exit() {
            for (Activity activity : activities) {
                if (activity != null) {
                        activity.finish();
                }
            }
        }
    private class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
    
    @Override
    
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    
    GApplication.getInstance().addActivity(activity);
    
    }
    
    @Override
    
    public void onActivityStarted(Activity activity) {
    
    }
    
    @Override
    
    public void onActivityResumed(Activity activity) {
    
    }
    
    @Override
    
    public void onActivityPaused(Activity activity) {
    
    }
    
    @Override
    
    public void onActivityStopped(Activity activity) {
    
    }
    
    @Override
    
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    
    }
    
    @Override
    
    public void onActivityDestroyed(Activity activity) {
    
    GApplication.getInstance().finishActivity(activity);
    
    }
    
    }
    }
    

    相关文章

      网友评论

          本文标题:关闭 SDK 中的Activity ActivityLifecy

          本文链接:https://www.haomeiwen.com/subject/egbzfxtx.html