间的服务,Web和活动,来回走动,在Android的交际 [英] Comunication between Service, Web and Activity, back and forth, in Android

查看:150
本文介绍了间的服务,Web和活动,来回走动,在Android的交际的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了大部分的问题和答案周围SO,我必须说,我仍然不知道哪些路可走,在我的情况。有几种方法一起去,但我真的不能决定哪一个是最适合我的情况。

我已经有了

我有一个会从我的应用程序启动的服务。它检查用户的当前位置,并调用与读出的位置的Web服务。出现这种情况每隔x米。

我想获得什么


  • 有与地图的活性,并显示从该服务的当前的位置的读数。这意味着我的活动需要在每个位置读取予以通报。请记住,只有当用户启动活动,这将使用,所以我不认为从服务广播的每个位置将是一个不错的主意。

  • 一个活动,我可以要求服务的最后读出的位置。

  • 正如我前面提到的,服务读取位置,需要将其发送到Web服务器每隔几秒钟。什么是我们的最佳方法呢?在服务做出新的Thread()为每个Web服务调用?使用AsyncTask的?有时,我可能会收到一个回应一些额外的价值,情况,我需要从WebService开始一个活动。

有了这个说,你有什么建议我使用?这会是一个很好的方法:<一href=\"http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging\">Example:使用消息活动和服务之间的通信?

LE:我从IntentService切换到简单的服务+线程将数据发送到Web服务。我IntentService开始实施,并迅速给摧毁了。也许是我做错了什么......


解决方案

  

这意味着我的活动需要在每个位置读取予以通报。


我认为,你的目标廉洁高效的解决方案是使用 ResultReceiver
搜索结果注意:(娄我会告诉你例如,从我的应用程序)

示例:

从你的呼叫活动的服务

 意向书I =新意图(这一点,DownloadService.class);
i.putExtra(URL,http://dl.dropbox.com/u/67617541/2011-11-11_102220_nature.jpg);
i.putExtra(接收器,新DownloadReceiver(新的处理程序()));
startService(ⅰ);

实施 ResultReceiver

 私有类DownloadReceiver扩展ResultReceiver {        公共DownloadReceiver(处理程序处理){
            超(处理);
        }        @覆盖
        公共无效onReceiveResult(INT结果code,捆绑resultData){
           super.onReceiveResult(结果code,resultData);
           如果(结果code == DownloadService.PROGRESS_UPDATE){
              INT进度= resultData.getInt(进步);
              pd.setProgress(进度);
           }
       }
}

和片段的 IntentService

 保护无效onHandleIntent(意向意图){
   字符串urlLink = intent.getStringExtra(URL);
   ResultReceiver接收器= intent.getParcelableExtra(接收器);
   //一些身体
   捆绑数据=新包();
   //出版进度
   receiver.send(PROGRESS_UPDATE,数据);
}

所以,简单地通过调用发送(当你需要)方法将被称为 onReceiveResult ResultReceiver 在这里你可以做你的需要。

I read most of the questions and answers around SO and I must say I still have no idea on which way to go, in my case. There are several ways to go with but I can't really decide which one is best suited for my case.

What I've got

I have an Service that gets started from my app. It checks the current location of the user and calls a web service with the read location. This occurs every x meters.

What I want to obtain

  • Have an activity with a map and display the current location readings from the service. This means my Activity needs to be informed on each location read. Keep in mind that this will be use only when the user starts the activity so I don't think that broadcasting each location from the service would be a good idea.
  • An activity where I can ask the service for its last read location.
  • As I mentioned before, the service reads location and needs to send it to a web server every few seconds. What would be the best approach for this ? In Service make a new Thread() for each web service call ? Use AsyncTask ? Sometimes I may receive as a response some extra values, case in which I need to start an Activity from the webservice.

Having this said, what would you recommend me to use ? Would this be a good approach: Example: Communication between Activity and Service using Messaging ?

LE: I have switched from IntentService to simple Service + thread for sending data to the web service. My IntentService implementation started and quickly for destroyed. Probably I did something wrong....

解决方案

This means my Activity needs to be informed on each location read.

I think that a clean and efficient solution for your goal is to use ResultReceiver.

Note: (Bellow i will show you example from my application).

Example:

Activity from its you call Service.

Intent i = new Intent(this, DownloadService.class);
i.putExtra("url", "http://dl.dropbox.com/u/67617541/2011-11-11_102220_nature.jpg");
i.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(i);

Implemented ResultReceiver

private class DownloadReceiver extends ResultReceiver {

        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        public void onReceiveResult(int resultCode, Bundle resultData) {
           super.onReceiveResult(resultCode, resultData);
           if (resultCode == DownloadService.PROGRESS_UPDATE) {
              int progress = resultData.getInt("progress");
              pd.setProgress(progress);
           }
       }
}

And fragment of IntentService

protected void onHandleIntent(Intent intent) {  
   String urlLink = intent.getStringExtra("url");
   ResultReceiver receiver = intent.getParcelableExtra("receiver");
   // some body
   Bundle data = new Bundle();
   //publishing progress
   receiver.send(PROGRESS_UPDATE, data);
}

So simply by calling send (when you need) method will be called onReceiveResult in your ResultReceiver where you can do what you need.

这篇关于间的服务,Web和活动,来回走动,在Android的交际的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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