0

以下是我的類GCMIntentService的代碼,當通知到達手機時會調用它。我的問題如下:Android GCM:自動喚醒不起作用

當通知到達電話時,他醒了,但它永遠不會返回到待機模式。

我認爲問題是圍繞着wakelock,但不知道它是什麼。

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

    } 
} 

回答

3

更改爲

wl.acquire(15000); 
wl.acquire(); 
+0

我會盡力。但我已經看到我沒有插入wl.release();我必須那樣做? – pxrb66

+0

謝謝你!它解決了我的問題! – pxrb66