gpt4 book ai didi

android - Activity 监听器 - 谷歌云消息传递 - BroadcastReceiver

转载 作者:太空狗 更新时间:2023-10-29 12:45:54 25 4
gpt4 key购买 nike

我已经在我的 android 应用程序中实现了 GCM,它可以正常接收消息。BroadcastReceiver 是根据 Google 提供的示例在 list 文件中设置的。

我的问题如下:如果用户正在打开应用程序,而我想更新该 View 中的一些结果 - 如何做到这一点?我首先考虑将此 Activity 注册为 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);
}
}

这是 list 文件中的重要部分:

       <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。检查 Activity 是否正在运行。

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

blog将使您了解如何实现这一目标。

关于android - Activity 监听器 - 谷歌云消息传递 - BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18311492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com