活动监听器 - Google云消息传递 - BroadcastReceiver [英] Activity Listener - Google Cloud Messaging - BroadcastReceiver
问题描述
我在我的android应用程序中实现了GCM,并且在接收消息时工作正常。
根据Google提供的示例,BroadcastReceiver设置在清单文件中。
我的问题如下:如果用户有应用程序打开并且我想更新该视图中的一些结果 - 这怎么做?我首先想到在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构建器;
public GCMIntentService(){
super(GCMIntentService);
@Override
protected void onHandleIntent(意图意图){
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
字符串messageType = gcm.getMessageType(intent);
$ b $ if if(!extras.isEmpty()){
if(GoogleCloudMessaging。
MESSAGE_TYPE_MESSAGE.equals(messageType)){
/ ** How检查活动
GameActivity是否正在运行,并因此发送一个
更新信号给它?如果没有运行,应该创建一个
通知。
** /
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
这是清单文件中的重要部分:
< receiver
android:name =qwegcm.GCMBroadcastReceiver
android:permission = com.google.android.c2dm.permission.SEND>
< intent-filter>
< category android:name =q.w/>
< / intent-filter>
< / receiver>
< service android:name =q.w.e.gcm.GCMIntentService/>
有什么建议?
谢谢! / p>
解决方案有两种方法可以处理这个问题。 1。检查活动是否正在运行。
ActivityManager am =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
列出< RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName()。equalsIgnoreCase(com.yourpackagename)){
// Activity正在运行
发送包含您在活动中注册的意图过滤器的广播
您想要更新的位置
}
else {
//活动未运行
//生成通知
}
2。使用 SendOrderedBroadcast
。
blog 会给你一个关于如何实现的想法。
I have implemented GCM in my android application and it's working fine with receiving messages.
The BroadcastReceiver is set up in the manifest-file according to the examples provided by Google.
My question is the following: If the user is having the application open and I want to update some results in that view - how can this be done? I was first thinking of registering this activity as a listener on whatever the BroadCastReceiver recieves. However, this must then be a static list of listeners, as a new instance of the BroadcastReceiver will be set up - but maybe this is not the way to do this.
This is what I currently have
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);
}
}
Here's the important part for this in the manifest-file:
<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" />
Any advice?
Thanks!
解决方案 There are two ways to handle this.
1. Check if activity is running.
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. Using SendOrderedBroadcast
.
This blog will give you an idea on how this could be achieved.
这篇关于活动监听器 - Google云消息传递 - BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!