应用程序不让设备进入睡眠模式

时间:2013-10-15 14:27:28

标签: android broadcastreceiver android-service

我正在开发一个应用程序,其中我正在使用activity,它将位于设备的默认锁定屏幕之上。一切都很好,但我遇到display timeout of the screen的问题。每当我的activity到来时,设备都不会进入睡眠模式。为什么呢?

以下是Service类和BroadcastReceiver类的代码段。我可以找出阻碍设备屏幕超时模式的原因。

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;
        }
    }
};

我无法找到让设备与我的应用程序一起睡觉的原因。

任何形式的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

通过添加Clear All Flags来保持屏幕显示来解决此问题。 这是我使用的代码:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);