2012-09-03 52 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