zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    随机数生成
    C#根据流下载文件
    C# 改变Windows中服务的登录身份 (账户名和密码)
    SqlServer查看数据库信息及服务器级、数据库级、数据库独立 用户权限
    C# 两个List<T>(T是类)使用Contains比较元素的方法
    你真正了解public、internal、protected、private吗?不妨幽默一些
    【WCF Restful】Post传参示范
    VMware虚拟机可与Win10物理机互ping并可访问互联网的设置方法
    服务器Oracle数据库配置与客户端访问数据库的一系列必要设置
    Winform ListBox输出信息并自动滚动至底部
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/9379015.html
Copyright © 2011-2022 走看看