首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android事件日志

Android事件日志
EN

Stack Overflow用户
提问于 2015-07-21 04:59:33
回答 1查看 232关注 0票数 0

我正在寻找沃德,以记录一些事件触发的活动,如onClick,editTextChanged,onTouch没有任何额外的努力,从程序员提供了一个库。有没有什么方法可以做到这一点,而不需要他在他的事件处理程序中放置一些注释?例如,我可以创建一个超级活动,并告诉他扩展它。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-07-21 07:29:38

我是从Coursera的移动云计算课程中得到这个想法的。该类提供的示例代码中的每个活动都扩展了这一点:

代码语言:javascript
复制
/**
 * This abstract class extends the Activity class and overrides lifecycle
 * callbacks for logging various lifecycle events.
 */
public abstract class LifecycleLoggingActivity extends Activity {
/**
 * Debugging tag used by the Android logger.
 */
protected final String TAG = getClass().getSimpleName();

/**
 * Hook method called when a new instance of Activity is created. One time
 * initialization code should go here e.g. UI layout, some class scope
 * variable initialization. if finish() is called from onCreate no other
 * lifecycle callbacks are called except for onDestroy().
 * 
 * @param savedInstanceState
 *            object that contains saved state information.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // Always call super class for necessary
    // initialization/implementation.
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        // The activity is being re-created. Use the
        // savedInstanceState bundle for initializations either
        // during onCreate or onRestoreInstanceState().
        Log.d(TAG, "onCreate(): activity re-created");

    } else {
        // Activity is being created anew. No prior saved
        // instance state information available in Bundle object.
        Log.d(TAG, "onCreate(): activity created anew");
    }

}

/**
 * Hook method called after onCreate() or after onRestart() (when the
 * activity is being restarted from stopped state). Should re-acquire
 * resources relinquished when activity was stopped (onStop()) or acquire
 * those resources for the first time after onCreate().
 */
@Override
protected void onStart() {
    // Always call super class for necessary
    // initialization/implementation.
    super.onStart();
    Log.d(TAG, "onStart() - the activity is about to become visible");
}

/**
 * Hook method called after onRestoreStateInstance(Bundle) only if there is
 * a prior saved instance state in Bundle object. onResume() is called
 * immediately after onStart(). onResume() is called when user resumes
 * activity from paused state (onPause()) User can begin interacting with
 * activity. Place to start animations, acquire exclusive resources, such as
 * the camera.
 */
@Override
protected void onResume() {
    // Always call super class for necessary
    // initialization/implementation and then log which lifecycle
    // hook method is being called.
    super.onResume();
    Log.d(TAG,
            "onResume() - the activity has become visible (it is now \"resumed\")");
}

/**
 * Hook method called when an Activity loses focus but is still visible in
 * background. May be followed by onStop() or onResume(). Delegate more CPU
 * intensive operation to onStop for seamless transition to next activity.
 * Save persistent state (onSaveInstanceState()) in case app is killed.
 * Often used to release exclusive resources.
 */
@Override
protected void onPause() {
    // Always call super class for necessary
    // initialization/implementation and then log which lifecycle
    // hook method is being called.
    super.onPause();
    Log.d(TAG,
            "onPause() - another activity is taking focus (this activity is about to be \"paused\")");
}

/**
 * Called when Activity is no longer visible. Release resources that may
 * cause memory leak. Save instance state (onSaveInstanceState()) in case
 * activity is killed.
 */
@Override
protected void onStop() {
    // Always call super class for necessary
    // initialization/implementation and then log which lifecycle
    // hook method is being called.
    super.onStop();
    Log.d(TAG,
            "onStop() - the activity is no longer visible (it is now \"stopped\")");
}

/**
 * Hook method called when user restarts a stopped activity. Is followed by
 * a call to onStart() and onResume().
 */
@Override
protected void onRestart() {
    // Always call super class for necessary
    // initialization/implementation and then log which lifecycle
    // hook method is being called.
    super.onRestart();
    Log.d(TAG, "onRestart() - the activity is about to be restarted()");
}

/**
 * Hook method that gives a final chance to release resources and stop
 * spawned threads. onDestroy() may not always be called-when system kills
 * hosting process
 */
@Override
protected void onDestroy() {
    // Always call super class for necessary
    // initialization/implementation and then log which lifecycle
    // hook method is being called.
    super.onDestroy();
    Log.d(TAG, "onDestroy() - the activity is about to be destroyed");
}

}

它是由Vanderbilt大学为课程开发的,是简化活动的框架的一部分。我手头没有许可证,但基本上你可以使用它,但你不能以任何方式声称你的应用程序或代码是由Vanderbilt大学或Coursera背书的。

每个扩展此活动的活动都将自动记录其所有生命周期方法,并使用类名进行标记。

编辑

如您所提到的,要使用来自接口的方法完成此操作,您需要创建该接口的一个实现,该接口遵循相同的思想,并将其用作活动中的变量,而不是直接在活动中实现该接口。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31526260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档