0

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);
    }
}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58

1 Answers1

0

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();
MFP
  • 1,141
  • 9
  • 22
  • the phone is wake up. but it still doesn't receive any message when i press the power button. i'm even have put it on the service with looping statement to make it always wakeup, but still doesn't receive the message, but when i unlock the phone the message directly come. – user3905216 Aug 04 '14 at 06:30
  • Actually what i want is the GCM will work or still able to receive the message even my phone is in locked mode, sleep or etc except shutdown such as BBM, wassap line application that still able to receive the message even the phone is in off mode except shutdown. thanks in advance guys. – user3905216 Aug 04 '14 at 06:36