当设备的屏幕在android中解锁时如何重启活动?

时间:2017-05-26 08:13:54

标签: android android-activity locking keyevent

我有4项活动:

  • FirstActivity - 它在应用运行的第一次显示
  • MainActivity
  • SeconActivity
  • ThirdActivity

我想在点击电源按钮并用户解锁他/她的手机时显示FirstActivity。但是如何在显示FirstActivity后再次显示BackgroundActivity之后如何处理它。 我怎么能这样做?

例如MainActivity

 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        Intent checkingIntent=new Intent(getApplicationContext(),FirstActivity.class);
        checkingIntent.putExtra("checking",true);
        startActivity(checkingIntent);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

并在FirstActivity中

public boolean checking() {
    checking_FOR_bankInfo= getIntent().getExtras().getBoolean("checking");
    if (checking_FOR_bankInfo){
        ...
    }
    return cdo_state;
}

2 个答案:

答案 0 :(得分:1)

在每个活动onPause()方法中尝试以下代码:

@Override
protected void onPause() {
    super.onPause();

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    boolean screenOn;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        screenOn = pm.isInteractive();
    } else {
        screenOn = pm.isScreenOn();
    }
    if (!screenOn) {    //Screen off by lock or power
        Intent checkingIntent = new Intent(this, FirstActivity.class);
        checkingIntent.putExtra("checking",true);
        checkingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(checkingIntent);
        finish();
    }
}

答案 1 :(得分:-1)

@Override
    protected void onStop() 
    {
        super.onStop();
        Log.d(tag, "MYonStop is called");
        // insert here your instructions
        // you can call new activity here.


    }

当您按下HOME按钮时,将调用onStop()方法。