Android:在广播接收器上创建重复活动

Android:在广播接收器上创建重复活动,android,android-broadcast,Android,Android Broadcast,我在我的gcminent服务类中指定了一个活动,每当用户从服务器接收广播时,都需要打开该活动。下面是我的GCMINENT类中的代码: private void sendNotification(String msg) { String payload=msg.substring(msg.indexOf("RequestResponse=")+20, msg.indexOf("from")-2); mNotificationManager = (NotificationManager) this.

我在我的gcminent服务类中指定了一个活动,每当用户从服务器接收广播时,都需要打开该活动。下面是我的GCMINENT类中的代码:

private void sendNotification(String msg) {
String payload=msg.substring(msg.indexOf("RequestResponse=")+20, msg.indexOf("from")-2);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ListActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("xxx").setStyle(new NotificationCompat.BigTextStyle().bigText("Update List")).setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
所以,在广播消息“单击我的列表”时,活动应该打开,它将根据从服务器接收到的数据刷新列表。问题是,如果我的ListActivity在前台打开,那么在广播时再次单击ListActivity,然后我实际上有两个相同活动的实例,一个在另一个上。如果前景中有其他活动,则其工作正常。 我做了一些研究,似乎我们不应该使用代码来确定生产中的前景活动。一些帖子建议,使用ActivityManager了解当前的前台活动仅用于调试目的。
我知道这一点

将其添加到清单文件中的ListActivity中

android:launchMode="singleInstance"

编辑:由于将活动从后台带回时总是调用
onResume()
,因此您可以使用直接在
onResume()
中收到的数据更新列表,而不是在其他地方。

谢谢您的建议,但这不起作用。对于接收到的每个广播,我必须刷新ListActivity显示的列表。使用上述解决方案,在某种程度上冻结活动本身,因为它已经打开,无法更新任何内容。由于从后台带回活动时总是调用
onResume()
,因此您可以使用直接在
onResume()中接收的数据更新列表
其他地方都没有。@Luciano:你因为缩进而否决了这个问题吗?我没有否决你的问题@Gaurav@LucianoRodr好的,我想是因为缩进的缘故。抱歉打扰你了。