Note that there are some explanatory texts on larger screens.

plurals
  1. POshake screen unlock
    text
    copied!<p>I am working on an app which will lock the screen on shake it consist of a main activity having two buttons to start and stop the app. After that it is entering into a broadcast receiver which is checking whether the screen is on or off and then redirecting to a service.For first 3 or 4 shake lock and unlock its working perfectly after that its executing indefinitely.</p> <p>The broadcast receiver i am using is..</p> <pre><code> public void onReceive(Context context, Intent intent) { System.out.println("Entered Broadcaste Reciever"); if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // DO WHATEVER YOU NEED TO DO HERE wasScreenOn = "1"; System.out.println("Check"+"Screen went OFF"); Intent i = new Intent(context, UpdateService.class); i.putExtra("screenstate", wasScreenOn); context.startService(i); System.out.println("jrkejhr keh"); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { // AND DO WHATEVER YOU NEED TO DO HERE wasScreenOn = "2"; Log.i("Check","Screen went ONN"); System.out.println("Entering to the service................."); Intent ScreenOnIntent = new Intent(context,UpdateService.class); ScreenOnIntent.putExtra("screenstate", wasScreenOn); context.startService(ScreenOnIntent); } } </code></pre> <p>and the service is.....</p> <pre><code> public void onStart(Intent intent, int startId) { System.out.println("Entered On Start"); data=intent.getStringExtra("screenstate"); System.out.println("Data from Broadcast Receiver is"+data); mShaker = new ShakeListener(this); mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () { public void onShake() { if(data.equals("1")) { System.out.println("............................................."); PowerManager TempPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock TempWakeLock = TempPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "TempWakeLock"); TempWakeLock.acquire(); final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); vibe.vibrate(100); } else { final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); System.out.println("The screen is off and is shaking........."); vibe.vibrate(100); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new ScreenReceiver(); registerReceiver(mReceiver, filter); ComponentName mAdminName = new ComponentName(UpdateService.this,LockActivity.class); System.out.println("The Device device admin enabled"); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled"); mDPM.lockNow(); mDPM.setMaximumTimeToLock(mAdminName,0); intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK); // startActivityForResult(intent, 1); } } }); } </code></pre> <p>I want to execute this only once..if screen is on execute once and put screen off,and viceversa..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload