如何正确唤醒屏幕服务?

时间:2015-02-28 20:59:56

标签: android

我有IntentService,我在哪里创建通知。我希望在创建通知后打开屏幕。如果PowerManager.WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.FULL_WAKE_LOCK, "example");被折旧,我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我的问题和你的问题一样但是下面的代码对我有用..

在你的包中创建一个WakeLocaker.java类

import android.content.Context;
import android.os.PowerManager;

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "WakeLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

并在您的活动 private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() 中调用以下行代码

然后调用释放锁定方法

// Releasing wake lock
WakeLocker.release();

不要忘记在menifest.xml中添加权限

<uses-permission android:name="android.permission.WAKE_LOCK" />

此代码信用完全属于 http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/