0

我正在开发幻灯片功能,这种停止和重新启动会在幻灯片流程中产生问题,我们可以更改它吗?

生命周期方法的日志

07-04 22:47:27.944: D/ReceiverActivity(7365): Activity Life cycle - onStart called
07-04 22:47:27.944: D/ReceiverActivity(7365): Activity Life cycle - onResume called
07-04 22:47:28.467: D/ReceiverActivity(7365): Activity Life cycle - onStop called
07-04 22:47:29.139: D/ReceiverActivity(7365): Activity Life cycle - onRestart called
07-04 22:47:29.139: D/ReceiverActivity(7365): Activity Life cycle - onStart called
07-04 22:47:29.139: D/ReceiverActivity(7365): Activity Life cycle - onResume called

这是我的应用代码片段

@Override
public void onReceive(final Context context, final Intent intent) {
    if(action.equals(Intent.ACTION_SCREEN_OFF)) {
        Intent ri = new Intent(context, ReceiverActivity.class);
            ri.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(ri);
    }
}


public class ReceiverActivity extends Activity
{
    StringBuilder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        // Turn on the screen unless we are being launched from the AlarmAlert
        // subclass as a result of the screen turning off.
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.launcher_layout);
        builder = new StringBuilder();
        builder.append("onCreate called.");
        Log.e("ReceiverActivity", "String : " + builder.toString());
    }

    @Override
    protected void onStart() {
        Log.d("ReceiverActivity","Activity Life cycle - onStart called");
        super.onStart();
        builder.append("\nonStart called.");
    }

    @Override
    protected void onResume() {
        Log.d("ReceiverActivity","Activity Life cycle - onResume called");
        super.onResume();
        builder.append("\nonResume called.");
        Log.e("ReceiverActivity", "String : " + builder.toString());
        ((TextView) findViewById(R.id.textView1)).setText(builder.toString());
    }
    @Override
    protected void onRestart() {
        Log.d("ReceiverActivity","Activity Life cycle - onRestart called");
        super.onRestart();
    }
    @Override
    protected void onStop() {
        Log.d("ReceiverActivity","Activity Life cycle - onStop called");
        super.onStop();
    }
}

提前致谢

4

1 回答 1

0

在我看来,您需要知道在调用 onResume 时屏幕实际上是打开还是关闭,以确定您应该如何处理该事件。您要检查:

  • 您的活动处于前台。
  • 屏幕亮着。

获取屏幕是否打开/关闭以及应用程序是否在后台/前台为如何执行这两个操作提供了明确的答案。

于 2013-07-06T18:58:34.783 回答