Android pops up an Activity after brightening the screen

First we need to set permissions

<uses-permission android:name="android.permission.WAKE_LOCK" />

Then register the broadcast dynamically:

 IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(new AlarmClockReceiver(), filter);

Broadcast reception processing ( AlarmClockReceiver)

public class AlarmClockReceiver extends BroadcastReceiver {
    
    

    private String TAG="hyc";

    @Override
    public void onReceive(Context context, Intent intent) {
    
    
      if (Intent.ACTION_SCREEN_ON==intent.getAction()){
    
    //亮屏
         Intent startPhone = new Intent(context, PhotoActivity.class);
          startPhone.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(startPhone);
          Log.d(TAG, "onReceive:on ");
      }else if (Intent.ACTION_SCREEN_OFF==intent.getAction()){
    
    //灭屏
          Log.d(TAG, "onReceive:off ");

      }
    }
}

The manifest files for broadcast and service are as follows:

 <service
            android:name=".RemindService"
            android:enabled="true"
            android:exported="true" />
        <receiver
            android:name=".AlarmClockReceiver"
            android:enabled="true"
            android:exported="true"
          />

The operations we need to show the Activity need to perform

public class PhotoActivity extends AppCompatActivity {
    
    

    @SuppressLint("InvalidWakeLockTag")
    private PowerManager.WakeLock wl;

    @SuppressLint("InvalidWakeLockTag")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    
    
            setShowWhenLocked(true);
        }else{
    
    
            //页面悬浮于锁屏之上
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

            //亮屏
            KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unlock");
            kl.disableKeyguard();

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "bright");
            wl.acquire();
        }
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        if (wl!=null){
    
    
            wl.release();
        }

    }
}

My blog will be synchronized to the Tencent Cloud+ community, and everyone is invited to join: https://cloud.tencent.com/developer/support-plan?invite_code=3t68h9hdbicks

Guess you like

Origin blog.csdn.net/weixin_44710164/article/details/107945936