简体   繁体   中英

GCM isn't receiving the message after some second when the power button pressed (SLEEP)

Please help me to solve this problem guys.. The phone only receive message when the device is on. When it's off for some second around(20-30) by pressing the power button then it's stop receiving and will continue to recieve when it's on. This is my receiver.

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

create one class like below for 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;
}
}

and write this lines in onrecive method of BroadcastReceiver

// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
// Releasing wake lock
WakeLocker.release();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM