【问题标题】:GCM isn't receiving the message after some second when the power button pressed (SLEEP)按下电源按钮 (SLEEP) 几秒钟后,GCM 未收到消息
【发布时间】:2014-08-04 04:33:25
【问题描述】:

请帮我解决这个问题。 手机仅在设备开启时才会收到消息。 当它通过按下电源按钮关闭大约(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);
    }
}

【问题讨论】:

    标签: java android android-broadcastreceiver


    【解决方案1】:

    为 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();
    

    【讨论】:

    • 手机已唤醒。但是当我按下电源按钮时它仍然没有收到任何消息。我什至用循环语句把它放在服务上,让它总是唤醒,但仍然没有收到消息,但是当我解锁手机时,消息直接来了。
    • 实际上我想要的是 GCM 将工作或仍然能够接收消息,即使我的手机处于锁定模式、睡眠等状态,除了关机(如 BBM、wassap 线路应用程序)仍然能够接收消息即使手机处于关机模式,除了关机。在此先感谢各位。
    猜你喜欢
    • 2018-06-07
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2017-10-13
    相关资源
    最近更新 更多