使用IntentService更新运行活动 [英] Update running Activity using IntentService

查看:124
本文介绍了使用IntentService更新运行活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是工作真正伟大的GCM-通知!在我的 IntentService 的GCM-通知我用下面的code创建一个通知:

  / **
 *
 * /
私人无效sendMessagesUpdatedNotification(){
    意图resultIntent =新意图(这一点,MessageActivity.class);
    resultIntent.putExtra(Globals.KEY_UPDATED,真);
    的PendingIntent resultPendingIntent = PendingIntent.getActivity(这一点,0,resultIntent,PendingIntent.FLAG_ONE_SHOT);    。字符串title = this.getResources()的getString(R.string.text_messages_updated);
    字符串消息= this.getResources()的getString(R.string.text_messages_updated_message)。    NotificationCompat.Builder建设者= this.buildNotification(标题,邮件);
    NotificationManager notificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);    builder.setSmallIcon(R.drawable.notification_ic_message);
    builder.setContentIntent(resultPendingIntent);
    builder.setAutoCancel(真);    notificationManager.notify(MESSAGES_UPDATED_ID,builder.build());
}

如果我在我的活动得到启动(或重新加载,如果畲族已经运行)通知点击。一切都很好。除了一件事:

我想有另一特点。如果我的活动已经在我想我的内容自动,而无需点击通知被更新的前面。

这个现象的原因可能吗?


解决方案

  

这个现象的原因可能吗?


使用事件总线。

该服务职位,公交车的事件说:哎,我们得到了一个消息!。您的活性,而在前台,将被登记在总线上接收事件。如果它获得的情况下,它更新UI。但是,如果该活动没有在前台,也有事件总线实现的,它可以让你的服务的食谱知道该活动没有回应,所以你可以提高通知作为后备机制。

有三大事件总线实现,我所知道的:

该示例应用程序都是一样的核心应用程序,只是用不同的事件总线实现。我用 AlarmManager 来触发该服务,而不是一个GCM消息,但相同的概念适用于你的情况与GCM。

就个人而言,我喜欢greenrobot的为更灵活的选择线程EventBus,但所有这三个可以在你的情况下工作。

I am using GCM-Notifications which are working really great! In my IntentService for the GCM-Notifications i use the following Code to create a notification:

/**
 * 
 */
private void sendMessagesUpdatedNotification() {
    Intent resultIntent = new Intent(this, MessageActivity.class);
    resultIntent.putExtra(Globals.KEY_UPDATED, true);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);

    String title = this.getResources().getString(R.string.text_messages_updated);
    String message = this.getResources().getString(R.string.text_messages_updated_message);

    NotificationCompat.Builder builder = this.buildNotification(title, message);
    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    builder.setSmallIcon(R.drawable.notification_ic_message);
    builder.setContentIntent(resultPendingIntent);
    builder.setAutoCancel(true);

    notificationManager.notify(MESSAGES_UPDATED_ID, builder.build());
}

If i click on the Notification my Activity gets started (or reloaded if shes already running). Everything great. Except one thing:

I want to have another feature. If my Activity is already in the front i want my content to be updated automatically without having to click on the notification.

Is that somehow possible?

解决方案

Is that somehow possible?

Use an event bus.

The service posts an event to the bus saying "hey, we got a message!". Your activity, while in the foreground, would be registered on the bus to receive events. If it gets the event, it updates the UI. If, however, the activity is not in the foreground, there are recipes for event bus implementations that would let your service know that the activity did not respond, and so you can raise the Notification as a fallback mechanism.

There are three major event bus implementations that I know of:

The sample apps are all the same core app, just using different event bus implementations. I used AlarmManager to trigger the service rather than a GCM message, but the same concept applies in your case with GCM.

Personally, I like greenrobot's EventBus for the more-flexible threading options, but all three can work in your case.

这篇关于使用IntentService更新运行活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆