• Android官方架构组件介绍之应用(四)


    讲一个项目常见的功能,友盟统计功能

    例如一个项目有很多多modlue,每个里面modlue都有Activity,Activity需要友盟统一,Fragment也需要友盟统计。一般做法就是继承一个BaseActivity,BaseFragment。

    然后在BaseActivity,BaseFragment大概是这样的

    import android.support.v7.app.AppCompatActivity;
    
    public class BaseActivity extends AppCompatActivity {
    
        @Override
        protected void onStart() {
            super.onStart();
            MobclickAgent.onPageStart(getPageName(this));
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            MobclickAgent.onPageEnd(getPageName(this));
        }
    }

    但是如果你们代码,引入了其他的AAR代码,他们aar里面有Activity,但是你也该不了别人代码,但是你们需要统计他们Activity,这个时候怎么办?

    public class ActivityLifecycleMonitorCallback implements Application.ActivityLifecycleCallbacks {
        private static final boolean DEBUG = AppEnv.bAppdebug;
        private static final String TAG = DEBUG ? "ActivityLifecyMonitor" : ActivityLifecyMonitor.class.getSimpleName();
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityCreated  activity="+activity.hashCode());
            }
        }
    
        @Override
        public void onActivityStarted(Activity activity) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityStarted");
            }
            MobclickAgent.onPageStart(getPageName(activity));
     
        }
    
        @Override
        public void onActivityResumed(Activity activity) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityResumed");
            }
            MobclickAgent.onResume(activity);
        }
    
        @Override
        public void onActivityPaused(Activity activity) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityPaused");
            }
            MobclickAgent.onPause(activity);
        }
    
        @Override
        public void onActivityStopped(Activity activity) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityStopped");
            }
            MobclickAgent.onPageEnd(getPageName(activity));
        }
    
        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivitySaveInstanceState");
            }
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
            if (DEBUG) {
                Log.i(TAG, getPageName(activity) + " onActivityDestroyed  activity="+activity.hashCode());
            }
        }
    
        private String getPageName(Activity activity) {
            return activity.getClass().getName();
        }
    
    
    }

    然后在Application里面注册

     private void registerActivityLifecycleCallback(Application application) {
            ActivityLifecycleMonitorCallback  activityLifecycleMonitor = new ActivityLifecycleMonitorCallback();
            application.registerActivityLifecycleCallbacks(activityLifecycleMonitor);
        }
  • 相关阅读:
    006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
    005-Spring Boot配置分析-配置文件application、Environment、PropertySource、@Value、EnvironmentPostProcessor、Profiles
    新浪云php与java连接MySQL数据库
    基于网页内容数据采集 PHP开发学习笔记
    淘宝理财 中证500 中证300 基金收益计算
    响应式Web设计(Responsive Web design)
    FOJ 1075
    锐捷上网认证常见问题及解决办法
    session与cookie的区别
    php email邮箱正则验证
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/9379015.html
Copyright © 2020-2023  润新知