0

请帮我解决这个问题。手机只有在设备开机时才会收到消息。当它通过按下电源按钮关闭大约(20-30)秒时,它会停止接收并在它打开时继续接收。这是我的接收器。

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("GCM ", "GCM COME");
        ComponentName comp = new ComponentName(context.getPackageName(),
                MainService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}
4

1 回答 1

0

为 Wakelock 创建一个如下所示的类,

@SuppressLint("Wakelock")
public class WakeLocker {
private static PowerManager.WakeLock wakeLock;

@SuppressWarnings("deprecation")
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;
}
}

并在 BroadcastReceiver 的 onrecive 方法中写下这一行

// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
// Releasing wake lock
WakeLocker.release();
于 2014-08-04T04:52:51.770 回答