Note that there are some explanatory texts on larger screens.

plurals
  1. POApp not letting device to go into sleep mode
    text
    copied!<p>I'm developing an application in which I'm using an <code>activity</code> which will come on top of default lock screen of the device. All things are perfectly fine, but I'm having issues with <code>display timeout of the screen</code>. Every time when my <code>activity</code> comes, the device does not go into sleep mode. Why?</p> <p>Here is my code snippet for <code>Service</code> class and <code>BroadcastReceiver</code> class. I could find out what is hindering the device screen timeout mode.</p> <p><strong>BroadcastReceiver class</strong></p> <pre><code>@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { wasScreenOn = false; Intent intent11 = new Intent(context, LockActivity.class); intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent11); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { wasScreenOn = true; Intent intent11 = new Intent(context, LockActivity.class); intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent intent11 = new Intent(context, LockActivity.class); intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent11); } } </code></pre> <p><strong>Service Class</strong></p> <pre><code>public class PerkLockService extends Service { BroadcastReceiver mReceiver; @Override public IBinder onBind(Intent intent) { return null; } @SuppressWarnings("deprecation") @Override public void onCreate() { KeyguardManager.KeyguardLock k1; KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); k1 = km.newKeyguardLock("IN"); k1.disableKeyguard(); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); mReceiver = new PerkLockReceiver(); registerReceiver(mReceiver, filter); System.out.println("Service Created"); super.onCreate(); } @Override public void onDestroy() { System.out.println("Service Destroyed"); unregisterReceiver(mReceiver); stopSelf(); super.onDestroy(); } </code></pre> <p>Here is what I'm using <code>permissions</code> in the manifest</p> <pre><code>&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt; &lt;uses-permission android:name="android.permission.WRITE_SETTINGS" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.VIBRATE" /&gt; </code></pre> <p><strong>Update:</strong></p> <p>Adding my <code>MainAcitivty.java</code> from where I'm calling my service and <code>StateListener()</code> method:</p> <p><code>MainActivity.java</code></p> <pre><code>try { // initialize receiver startService(new Intent(this, PerkLockService.class)); StateListener phoneStateListener = new StateListener(); TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); } catch (Exception e) { e.printStackTrace(); } /** * Listen to the state of the phone, like ringing and alarm, and it * automatically dismiss the activity and show up the proper screen */ class StateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: finish(); break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("call Activity off hook"); finish(); break; case TelephonyManager.CALL_STATE_IDLE: break; } } }; </code></pre> <p>I could not find what is keeping the device to go to sleep with my app.</p> <p>Any kind of help will be appreciated.</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