我正在开发一个应用程序,在该应用程序中,我使用的activity
将位于设备的默认锁定屏幕之上。一切都很好,但我遇到了问题display timeout of the screen
。每次我activity
来的时候,设备都不会进入睡眠模式。为什么?
Service
这是我的类和类的代码片段BroadcastReceiver
。我可以找出阻碍设备屏幕超时模式的原因。
广播接收器类
@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);
}
}
服务等级
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();
}
这是我permissions
在清单中使用的
<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" />
更新:
MainAcitivty.java
从我调用我的服务和StateListener()
方法的位置添加我的:
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;
}
}
};
我找不到是什么让设备通过我的应用程序进入睡眠状态。
任何形式的帮助将不胜感激。