宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述Android官方架构组件介绍之应用相关的知识,希望能为你提供帮助。
讲一个项目常见的功能,友盟统计功能
【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) + " onActivityCreatedactivity="+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) + " onActivityDestroyedactivity="+activity.hashCode()); } }private String getPageName(Activity activity) { return activity.getClass().getName(); }}
然后在Application里面注册
private void registerActivityLifecycleCallback(Application application) { ActivityLifecycleMonitorCallbackactivityLifecycleMonitor = new ActivityLifecycleMonitorCallback(); application.registerActivityLifecycleCallbacks(activityLifecycleMonitor); }
推荐阅读
- cocos creatorandroid之微信开放平台修改签名baseResp.errCode=-6
- Android官方架构组件介绍之ViewModel
- Android官方架构组件介绍之LifeCycle
- Android官方架构组件介绍之LiveData
- Android 前台服务
- Tp5框架 关于App登录token唯一加密算法
- QQ空间怎样开通黄钻LV9?开通办法介绍
- TIM聊天窗口怎样取消置顶?_其它聊天
- 微信排版有啥技巧?微信公众号排版技巧分享_微信