Android GCM:自动唤醒不起作用

时间:2012-10-14 08:34:38

标签: android google-cloud-messaging power-management wakelock

这是我的类GCMIntentService的代码,当手机上收到通知时会调用它。我的问题如下:

当通知到达手机时,他会醒来,但之后它永远不会返回待机状态。

我认为问题在于唤醒锁,但不知道它是什么。

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService{

    public String regIdtoSend = ""; 

    public GCMIntentService(){
        super(Utils.GCMSenderId);
    }

    @Override
    protected void onError(Context context, String regId) {
        // TODO Auto-generated method stub
        Log.e("", "error registration id : "+regId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        // TODO Auto-generated method stub
        handleMessage(context, intent);
    }

    @Override
    protected void onRegistered(Context context, String regId) {
        // TODO Auto-generated method stub
        // Log.e("", "registration id : "+regId);
        handleRegistration(context, regId);
    }

    @Override
    protected void onUnregistered(Context context, String regId) {
        // TODO Auto-generated method stub

    }

    @SuppressWarnings({ "deprecation", "static-access" })
    private void handleMessage(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Utils.notiMsg = intent.getStringExtra("message");
        Utils.notiTitle = intent.getStringExtra("title");
        Utils.notiType = intent.getStringExtra("type");
        Utils.notiUrl = intent.getStringExtra("url");

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.ic_action_lab_white;
        CharSequence tickerText = Utils.notiMsg;
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        CharSequence contentTitle = "Nouveau deal";
        CharSequence contentText = Utils.notiMsg;
        Intent notificationIntent = new Intent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);

        Utils.notificationReceived=true;
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire();

    }
}

1 个答案:

答案 0 :(得分:3)

更改为

wl.acquire(15000);
wl.acquire();