36

我正在尝试使用 ActivityLifecycleCallbacks 自动捕获和记录 Android 生命周期事件,但是至少可以说,关于这件事的文档很少:

    public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

我不想扩展 Activity 类或覆盖现有的生命周期方法(onCreate、onResume 等),我希望有一个单独的类来监听这些事件并采取相应的行动。

有没有人有这方面的经验,或者有关于它如何工作的好的可靠文档或教程的链接?具体来说,ActivityLifecycleCallbacks如何注册,如何处理?

4

4 回答 4

57

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

getApplicaton().registerActivityLifecycleCallbacks(yourCustomClass);

此类将收到与您的个人活动相同的回调。祝你好运。

PS。这是 API 级别 14 顺便说一句,所以它不适用于旧手机。

于 2012-06-18T11:57:22.893 回答
36

我自己实现了Application.ActivityLifecycleCallbacks. 我正在使用SherlockActivity,但对于普通的 Activity 类可能会起作用。

首先,我正在创建一个接口,其中包含用于跟踪活动生命周期的所有方法:

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());
    }
}

第三,我正在创建一个从 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
        super.onResume();
        nMyApplication.onActivityResumed(this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        nMyApplication.onActivityPaused(this);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        nMyApplication.onActivityDestroyed(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        nMyApplication.onActivityStarted(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        nMyApplication.onActivityStopped(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        nMyApplication.onActivitySaveInstanceState(this, outState);
    }   
}

四、所有继承自SherlockActivity的类,我替换为MySherlockActivity:

public class MainActivity extends MySherlockActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

}

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

更新

此实现已通过 API 级别 9(姜饼)、API 级别 12(蜂窝)和 API 级别 17(果冻豆)进行了测试,并且运行良好。可能适用于 Android 的旧版本。

于 2013-01-23T00:12:00.460 回答
5
@Override
public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(MyApplication.this/*(Your Application Name)*/);
}

只在 Application 类上添加这一行,一切正常。

于 2017-11-16T07:18:18.973 回答
4

试试这个: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 或直接从应用程序菜单。这是通过EnumApplication 类中的一个来完成的:

public enum LaunchMechanism {
    DIRECT,
    NOTIFICATION,
    URL,
    BACKGROUND
}

private LaunchMechanism mLaunchMechanism = LaunchMechanism.DIRECT;

public void setLaunchMechanism(LaunchMechanism launchMechanism) {
    mLaunchMechanism = launchMechanism;
}

在我们的实现中,当我们启动一个将启动第三方活动的活动时,我们有标志,例如用户是否从我们的应用程序拨打电话或是否启动了浏览器。在启动活动中,onStop()我们然后进行这样的检查,以仅在这些标志为假时报告活动的不可见性:

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;
}

此解决方案不依赖于 API 级别,因此它应该一直工作到 API 级别 1。

于 2015-04-28T11:14:03.577 回答