我已经在我的 android 应用程序中实现了 GCM,它在接收消息方面工作正常。根据 Google 提供的示例,在清单文件中设置了 BroadcastReceiver。
我的问题如下:如果用户正在打开应用程序并且我想在该视图中更新一些结果 - 这怎么做?我首先考虑将此活动注册为广播接收器收到的任何内容的侦听器。但是,这必须是一个静态的侦听器列表,因为将设置一个新的 BroadcastReceiver 实例 - 但也许这不是这样做的方法。
这是我目前拥有的
public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
public class GCMIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMIntentService() {
super("GCMIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
/** How to check if the activity
GameActivity is running, and hence send an
update signal to it? If it's not running a
notification should be created.
**/
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
}
这是清单文件中的重要部分:
<receiver
android:name="q.w.e.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="q.w" />
</intent-filter>
</receiver>
<service android:name="q.w.e.gcm.GCMIntentService" />
有什么建议吗?
谢谢!