ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - Updating Fragments from an IntentService

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android之IntentService

参考: http://www.wheremylife.com/html/android-intentservice-getting-started/ http://blog.csdn.net/gaojie314/archive/2010/11/28/6040701.aspx IntentSer

The application that I'm working on has an activity with three fragments. Each fragment needs to show some data that is received from an IntentService.

Fragment 1 - the icon, the name and the description Fragment 2 - a list of articles Fragment 3 - a list of items

public class Activity extends SherlockFragmentActivity implements Actionbar.Tablistener
{

    public void onCreate(..) {

        ....
        performSearch();
        setupTabs(); // 3 tabs are setup, their clicks and swipes init'ed
        ...

    }

    @Override
    public void onResume() { 

      IntentFilter intentFilter = new IntentFilter(SearchRequestReceiver.ACTION);
      intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
      searchRequestReceiver = new SearchRequestReceiver(this);
      registerReceiver(searchRequestReceiver, intentFilter);

    }

    @Override 
    public void onPause() {

      unregisterReceivers();

    }
    ...

    public void performSearch() {

        Intent intent = new Intent(this, SearchRequest.class);
        intent.putExtra("searchTerm", this.searchTerm); // declared and initialised earlier
        startService(intent);
    }

    ...
}

What is the best way for me to push this data from my receiver to my fragments or am I approaching this the wrong way? I didn't use AsyncTasks because I wanted to decouple my services from the context or was that the wrong decision?

android android-fragments android-intentservice
asked Apr 27 '14 at 7:45 A23 1,161 1 9 22

 | 

2 Answers
2

解决方法

An EventBus is one of the neatest solutions in this situation. EventBus and Otto are both very easy to use.

An example using Otto...

Your IntentService

new Handler(Looper.getMainLooper()).post(new Runnable() {
   bus.post(new DataLoadCompleteEvent());
});

Note the necessity to post the event on Android's main thread with Otto. In this case, a DataLoadCompleteEvent could contain whatever you wanted.

推荐:Android:Service 之 IntentService(上)

       Service和其他的应用组件一样,运行在程序的主线程(UI线程),如果我们在Service中进行很多耗时或者阻塞的操作时,程序有可能会被挂起(ANR)。所以需要

Your Fragment

@Subscribe public void onLoad(DataLoadCompleteEvent event) {
    //Do stuff with event
}

Just make sure your Fragments register on the bus in their onResume(), and unregister in their onPause().


answered Apr 29 '14 at 5:50 Chris Horner 900 7 16

 | 

I've managed to get it working by having the different receivers within my fragments but it seems like an awful lot of repetition.

public class FragmentName extends SherlockFragment 
{

   private SearchRequestReceiver searchRequestReceiver;

   ..

   public class ServiceRequestReceiver extends BroadcastReceiver
   {

        @Override
        public void onReceive(Context context, Intent intent) {

            ...

        }

   }

}

Is there a better way?


edited Apr 29 '14 at 5:40 answered Apr 27 '14 at 13:40 A23 1,161 1 9 22

 | 

推荐:Android中的intentservice

在Android的应用中,往往需要在执行主界面的操作时,如果要执行耗时的操作,那么应该是另外开线程的,或者是用async或者handler,今天发现其实也可以用android中的一


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。