0

如果我的手机处于屏幕锁定状态,GCMBroadcast 接收器不会将消息发送到 GCMIntentService。我读过解决方案是在我的广播接收器的 onReceive 方法中实现wakelock.acquire() 和wakelock.release() 的技巧。但是 GCMBroadcastReceiver 最后有 onReceive,所以我不能添加这个功能。

有解决办法吗?

[编辑]

错误的问题,经过 5 小时的调查,我已经解决了所有问题。

澄清:

  • GCMBroadcast 有唤醒锁获取和释放,不需要任何实现。
  • 我的问题是由于在 SERVER CODE 中将 DELAY WHEN IDLE 属性设置为 true。

    builder = builder.delayWhileIdle(true);
    

来自官方文档:

如果设备已连接但空闲,消息仍将立即传递,除非 delay_while_idle 标志设置为 true。否则,它将存储在 GCM 服务器中,直到设备唤醒。

[/编辑]

这是我的代码: GCMIntentService

public class GCMIntentService extends GCMBaseIntentService{

public GCMIntentService() {
    super(CommonStuff.SENDERID);
    // TODO Auto-generated constructor stub
}


@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub
    System.out.println("onError:"+arg1);
}

@Override
protected void onMessage(Context context, Intent intent) {

    Log.i("GCMIntentService", "Message Received");

    Bundle extras = intent.getExtras();

    // Extract the payload from the message

    if (extras != null) {


        String type=(String)extras.get("type");
        String msg=(String)extras.get("message");
        //do something........

    }

}

@Override
protected void onRegistered(Context ctx, String regId) {
    Log.d("GCMIntentService","REGID_ARRIVED:"+regId);
    try {
        //do something
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
    System.out.println("onUnregistered:"+arg1);

}

}

唤醒锁

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

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

    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
    wakeLock.acquire();
}

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

我在清单中的 GCMreceiver

<receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="my.app.package" />
            </intent-filter>
        </receiver>
4

1 回答 1

0

只需将问题的编辑部分中的解决方案设置为已解决。

于 2012-10-25T06:33:00.687 回答