Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Activity
- protected void onResume() {
- super.onResume();
- registerBroadcastReceiver();
- }
- private void registerBroadcastReceiver() {
- final IntentFilter theFilter = new IntentFilter();
- theFilter.addAction(Intent.ACTION_SCREEN_ON);
- theFilter.addAction(Intent.ACTION_USER_PRESENT);
- theFilter.addAction(Intent.ACTION_SCREEN_OFF);
- BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String strAction = intent.getAction();
- KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
- if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
- if (myKM.inKeyguardRestrictedInputMode()) {
- System.out.println("Screen off " + "LOCKED");
- if (AdActivity.getInstance() == null) {
- startActivity(new Intent(MainActivity.this, AdActivity.class));
- }
- } else {
- if (AdActivity.getInstance() == null) {
- System.out.println("Screen off " + "UNLOCKED");
- startActivity(new Intent(MainActivity.this, AdActivity.class));
- }
- }
- }
- }
- };
- getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement