活动监听器 - Google云消息传递 - 广播接收器

3
我在我的安卓应用中实现了GCM功能,并且成功地接收了消息。BroadcastReceiver已经按照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);
            }
        }

以下是manifest文件中的重要部分:

在manifest文件中,以下是关键部分:

       <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" />

有什么建议吗?

谢谢!

1个回答

3

有两种处理方法。

1. 检查活动是否正在运行。

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("com.yourpackagename")){
    //Activity Running
    Send a broadcast with the intent-filter which you register in your activity
    where you want to have the updates
} 
else{
    //Activity Not Running
    //Generate Notification
}

2. 使用 SendOrderedBroadcast

这篇 博客 将为您提供如何实现此功能的想法。


我刚刚更新了我的问题,并提供了一些最小化的代码,展示它的外观。假设我有一个名为GameActivity的活动。使用您上面的代码,如果此活动正在运行,则我将知道其名称。但是,我应该如何将此广播到正在运行的实例? - moviaa
在GameActivity的onResume中注册广播接收器,在OnPause中取消注册。然后,您只需要使用sendBroadcast方法广播意图。如果您的活动正在运行,则能够捕获广播。 :) - Sunil Mishra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接