2012-09-03 35 views

回答

6

我建議在這裏使用廣播接收器。所以IntentService將廣播該事件,並且所有活動或其他東西都可以註冊。

爲了避免BroadcastReceivers本身矯枉過正(因爲它們是在intented進程間通信中使用,並且可以是矯枉過正和安全泄漏) - I reccoment使用LocalBroadcastManager - http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html。它是嚴格本地廣播的幫手,更容易處理它們並提供更高的性能和安全性。

更新:

要使用LocalBroadcastReceiver GCM IntentService裏面你應該做這樣的事情:

@Override 
protected void onMessage(Context context, Intent intent) { 
    Intent bIntent = new Intent("custom-event-name"); 
    // You can also include some extra data. 
    bIntent .putExtra("message", intent); //or get some info from Gcm intent and put it into this one. 
    LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent); 
} 

在這裏你可以查看如何註冊活動裏面的接收器,這是很容易: how to use LocalBroadcastManager?

好運

+0

感謝alexn,我是機器人的新手,請將修改應用於代碼並顯示我...對不起我的英語。 – Antonio

+0

其實,甚至沒有必要粘貼一個代碼 - 主題在這裏描述的細節:http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager希望它有助於 – AlexN

+0

但我不知道浩申請代碼爲什麼收到擴展GCMBaseIntentService ....請幫我... – Antonio