问题
I'm developing an application in which I'm using an activity
which will come on top of default lock screen of the device. All things are perfectly fine, but I'm having issues with display timeout of the screen
. Every time when my activity
comes, the device does not go into sleep mode. Why?
Here is my code snippet for Service
class and BroadcastReceiver
class. I could find out what is hindering the device screen timeout mode.
BroadcastReceiver class
@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);
}
}
Service Class
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();
}
Here is what I'm using permissions
in the manifest
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
Update:
Adding my MainAcitivty.java
from where I'm calling my service and StateListener()
method:
MainActivity.java
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;
}
}
};
I could not find what is keeping the device to go to sleep with my app.
Any kind of help will be appreciated.
回答1:
Solved the issue here by adding Clear All Flags
for keeping screen on.
Here is the code which I have used:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
来源:https://stackoverflow.com/questions/19383777/app-not-letting-device-to-go-into-sleep-mode