码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-07-27 19:31:55      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:bubuko   pen   undle   bool   pre   click   style   aar   onpause   

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

例如一个项目有很多多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);
    }

 

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

标签:bubuko   pen   undle   bool   pre   click   style   aar   onpause   

原文地址:https://www.cnblogs.com/mingfeng002/p/9379015.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!