如何在 android 中解锁设备屏幕后重新启动 activity?

how to restart activity when device's screen unlocked in android?

我有 4 个 activity:

我想在单击电源按钮和用户解锁 his/her phone 时显示 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;
}
@Override
    protected void onStop() 
    {
        super.onStop();
        Log.d(tag, "MYonStop is called");
        // insert here your instructions
        // you can call new activity here.


    }

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

在每个 activity 的 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();
    }
}