使用ActivityLifecycleCallbacks自动loggingAndroid生命周期事件?

我正在尝试使用ActivityLifecycleCallbacks自动捕获和loggingAndroid生命周期事件,但至less在这方面的文档是稀缺的:

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback) 

我不想延长活动类或重写现有的生命周期方法(onCreate,onResume等…)我期待有一个单独的类监听这些事件,并采取相应的行动。

有没有人有这方面的经验,或链接到良好的可靠的文件或教程如何工作? 具体来说,如何注册ActivityLifecycleCallbacks,以及如何处理它们?

Solutions Collecting From Web of "使用ActivityLifecycleCallbacks自动loggingAndroid生命周期事件?"

我做了我自己的Application.ActivityLifecycleCallbacks的实现。 我正在使用SherlockActivity ,但对于正常的活动类可能工作。

首先,我创build了一个接口,其中包含跟踪活动生命周期的所有方法:

 public interface ActivityLifecycleCallbacks{ public void onActivityStopped(Activity activity); public void onActivityStarted(Activity activity); public void onActivitySaveInstanceState(Activity activity, Bundle outState); public void onActivityResumed(Activity activity); public void onActivityPaused(Activity activity); public void onActivityDestroyed(Activity activity); public void onActivityCreated(Activity activity, Bundle savedInstanceState); } 

其次,我在我的应用程序的类中实现了这个接口:

 public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Override public void onCreate() { super.onCreate(); } @Override public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Override public void onActivityStarted(Activity activity) { Log.i("Tracking Activity Started", activity.getLocalClassName()); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Override public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Override public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Override public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } 

第三,我创build了一个从SherlockActivity扩展的类:

 public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Override protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Override protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Override protected void onStart() { nMyApplication.onActivityStarted(this); super.onStart(); } @Override protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Override protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } 

第四,从SherlockActivity扩展的所有类,我replace为MySherlockActivity:

 public class MainActivity extends MySherlockActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

现在,在logcat中,您将看到在MyApplication中进行的接口实现中编写的日志。

UPDATE

这个实现是从API级别9(姜饼),API级别12(蜂窝)和API级别17(果冻豆)进行testing,工作正常。 可能在Android的旧版本中工作。

我没有任何第一手的经验,但从API判断,你可以写自己的类,实现Application.ActivityLifecycleCallbacks接口,并在提供的Application类实例上注册该类

 getApplicaton().registerActivityLifecycleCallbacks(yourCustomClass); 

这个class级将收到与您的个人活动相同的回拨。 祝你好运。

PS。 这是API级别14顺便说一句,所以它不会在旧手机上工作。

试试这个: http : //engineering.meetme.com/2015/04/android-determine-when-app-is-opened-or-closed/#comment-202

它提出了一个AppForegroundStateManager ,每个活动都通过它的onStop()onStart()函数来报告,如下所示:

 @Override protected void onStart() { super.onStart(); AppForegroundStateManager.getInstance().onActivityVisible(this); } @Override protected void onStop() { AppForegroundStateManager.getInstance().onActivityNotVisible(this); super.onStop(); } 

你的Application类实现了一个这样的监听器:

 public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); AppForegroundStateManager.getInstance().addListener(this); } @Override public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) { if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND.equals(newState)) { // App just entered the foreground. Do something here! Log.i(TAG, "App Just Entered the Foreground with launch mechanism of: " + mLaunchMechanism); } else { // App just entered the background. Set our launch mode back to the default of direct. mLaunchMechanism = LaunchMechanism.DIRECT; } } } 

它还包括确定应用程序如何打开的提示和技巧 – 从通知,打开应用程序的URL或直接从应用程序菜单中打开。 这是通过Application类中的Enum完成的:

 public enum LaunchMechanism { DIRECT, NOTIFICATION, URL, BACKGROUND } private LaunchMechanism mLaunchMechanism = LaunchMechanism.DIRECT; public void setLaunchMechanism(LaunchMechanism launchMechanism) { mLaunchMechanism = launchMechanism; } 

在我们的实现中,我们有一个标志,当我们开始一个活动,将启动第三方活动,如用户从我们的应用程序打电话或浏览器启动。 在启动活动的onStop()我们做一个这样的检查,只在这些标志为false时才报告活动的不可见性:

 if(!flag_userLaunchedThirdPartyActivity){ AppForegroundStateManager.getInstance().onActivityNotVisible(this); } 

为了检查应用程序是否进入后台(例如,当设备的屏幕变暗或用户接到电话时),其工作原理如下:

 public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } 

该解决scheme不依赖于API级别,所以它应该一直回到API级别1。

 @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(MyApplication.this/*(Your Application Name)*/); 

// registerActivityLifecycleCallbacks(MyApplication.this / (Your Application Name) / //只在Application类中添加这一行所有的作品都很好

}