【发布时间】:2013-10-15 14:27:27
【问题描述】:
我正在开发一个应用程序,我在其中使用activity,它将出现在设备的默认锁定屏幕之上。一切都很好,但我遇到了display timeout of the screen 的问题。每次我的activity 来时,设备都不会进入睡眠模式。为什么?
这是Service 类和BroadcastReceiver 类的代码 sn-p。我可以找出阻碍设备屏幕超时模式的原因。
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;
}
}
};
我找不到是什么让设备与我的应用一起进入睡眠状态。
我们将不胜感激。
【问题讨论】:
-
您是否在 LockActivity 中获取唤醒锁?
-
不,我没有在任何地方使用它。
-
显示超时设置为多少?是否可能只是设置得相当大而您等待的时间不够长?
-
我的设备默认屏幕超时为 15 秒。当我在一个单独的项目中尝试这个时它工作正常。但这不适用于这个特定的项目。
-
在您的屏幕关闭意图接收器中记录一个日志,以查看它是否会触发。也许它正在尝试关闭屏幕,但该接收器正在重新启动可能正在转动屏幕的活动重新开始。它可能发生得如此之快,以至于它似乎永远不会关闭。
标签: android broadcastreceiver android-service