Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. Main Activity
  2.  
  3.  
  4.  
  5. protected void onResume() {
  6. super.onResume();
  7.  
  8. registerBroadcastReceiver();
  9. }
  10.  
  11.  
  12.  
  13. private void registerBroadcastReceiver() {
  14. final IntentFilter theFilter = new IntentFilter();
  15. theFilter.addAction(Intent.ACTION_SCREEN_ON);
  16. theFilter.addAction(Intent.ACTION_USER_PRESENT);
  17. theFilter.addAction(Intent.ACTION_SCREEN_OFF);
  18.  
  19. BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
  20. @Override
  21. public void onReceive(Context context, Intent intent) {
  22. String strAction = intent.getAction();
  23.  
  24. KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
  25.  
  26. if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
  27. if (myKM.inKeyguardRestrictedInputMode()) {
  28. System.out.println("Screen off " + "LOCKED");
  29. if (AdActivity.getInstance() == null) {
  30. startActivity(new Intent(MainActivity.this, AdActivity.class));
  31. }
  32. } else {
  33. if (AdActivity.getInstance() == null) {
  34. System.out.println("Screen off " + "UNLOCKED");
  35. startActivity(new Intent(MainActivity.this, AdActivity.class));
  36. }
  37. }
  38. }
  39. }
  40. };
  41.  
  42. getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement