Android 从服务启动活动并更新其内容,有什么方法吗?
我正在使用一项服务从CAN接收一些GCM消息。我将消息放入一个额外的文件中,并将其传递给我想要启动的类,然后在对话框中显示消息 但下一次我收到一条消息,它应该更新之前打开的意图的内容,问题是我遇到了一个异常,我的应用程序崩溃了,eclipse强迫我使用“FLAG_ACTIVITY_NEW_TASK”来解决崩溃问题,但这样我的“PendingEvent.FLAG_update_CURRENT”将被忽略 如何更新以前打开的意图?创建第三个活动并将数据传回fort在我看来是一个糟糕的解决方案 服务类别:Android 从服务启动活动并更新其内容,有什么方法吗?,android,android-intent,android-activity,notifications,google-cloud-messaging,Android,Android Intent,Android Activity,Notifications,Google Cloud Messaging,我正在使用一项服务从CAN接收一些GCM消息。我将消息放入一个额外的文件中,并将其传递给我想要启动的类,然后在对话框中显示消息 但下一次我收到一条消息,它应该更新之前打开的意图的内容,问题是我遇到了一个异常,我的应用程序崩溃了,eclipse强迫我使用“FLAG_ACTIVITY_NEW_TASK”来解决崩溃问题,但这样我的“PendingEvent.FLAG_update_CURRENT”将被忽略 如何更新以前打开的意图?创建第三个活动并将数据传回fort在我看来是一个糟糕的解决方案 服务类别
...
private void sendNotification(String msg, Bundle extras)
{
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, DriverDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// If this is a notification type message include the data from the message
// with the intent
if (extras != null)
{
intent.putExtras(extras);
intent.putExtra("message", msg);
intent.setAction("com.myGcm.NOTIFICATION");
startActivity(intent);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setTicker(msg)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
...
您需要的解决方案应该是
LocalBroadcastManager
。这很简单:只需在活动的onResume
中注册一个侦听器,然后从其他任何地方启动一个包含更新信息的意图。我附上了两个链接,它们应该会指引你;)
您希望在活动中更新什么类型的内容?@Aashish-我需要以列表的形式向最终用户显示所有传入的消息。从服务中,发出通知。将该通知设置为在单击时启动活动。