问题描述
我正在开发针对Android手机的应用程序,与一个JSON / REST Web服务通信。我需要让某些类型的呼叫定期向服务器检查一些信息。在这方面,我可能也需要查询GPS当前位置。我很犹豫不决使用本地服务,因为我不很清楚如何处理他们,其实我需要定期检索这些数据,并相应地刷新一个图形页面。我听说,我可以使用PendingIntents,在服务上,该数据相关联的一个有效载荷,并将其发送到广播接收器,解压数据并刷新UI,听说还,这是因为什么广播接收器是一个不好的设计方法旨在用于。没有任何人有一些有用的提示吗?
I am developing an app for android mobiles that communicates with a json/rest web service. I need to make certain kinds of calls periodically to the server to check for some information.Within that context I might need also to query the GPS for the current position. I'm quite undecided to use a Local Service, since I don't know very well how to deal with them, in fact I need to retrieve those data periodically and refresh a MapView accordingly.I heard that I can use PendingIntents,in the service, associate this data as a payload and send them to a broadcast receiver which unpack the data and refresh the UI, I heard also that this is a bad design approach because of what broadcast receiver are intended to be used for.does anybody have some useful hints?
推荐答案
首先,你必须处理与谷歌地图,因为你会显示一个图形页面。看一看这在安卓上mobiForge 使用谷歌地图。
first you have to deal with google maps since you will display a mapview. Have a look at thisUsing Google Maps in Android on mobiForge.
其次,你需要一个类,提供了对GPS数据。它是简单的获得位置数据,并利用消息处理器更新UI。下面是一个例子:
Second you need a class that provides gps data. It is simple to get location data and update UI using message handler. Here is an example:
public MyGPS implements LocationListener{
public LocationManager lm = null;
private MainActivity SystemService = null;
//lat, lng
private double mLongitude = 0;
private double mLatitude = 0;
public MyGPS(MainActivity sservice){
this.SystemService = sservice;
this.startLocationService();
}
public void startLocationService(){
this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
}
public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}
}
在您的onCreate方法使这个类的一个实例和LocationListener的开始监听GPS更新。但你不能访问LNG和纬度,因为你没有从你的行为知道wheather它们被设置或空。因此,你需要一个处理程序,将消息发送到您的主要活动时,纬度和经度设置:
In your onCreate method make an instance of this class and the locationlistener will start to listen for gps updates. But you can not access lng and lat since you do not know from your activity wheather they are set or null. So you need a handler that sends a message to your main activity when lat and lng are set:
修改在下面的方法:
public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
Message msg = Message.obtain();
msg.what = UPDATE_LOCATION;
this.SystemService.myViewUpdateHandler.sendMessage(msg);
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}
在您的主要活动补充一点:
In your main activity add this:
Handler myViewUpdateHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LOCATION:
//access lat and lng
}));
}
super.handleMessage(msg);
}
};
由于该处理器是在你的mapactivity您可以在处理程序本身轻松地更新你的用户界面。每次GPS数据是aviable一个消息被触发和被处理器接收到的
Since the handler is in your mapactivity you can update your UI easily in the handler itself. Everytime gps data is aviable a message is triggered and received by the handler.
开发一个REST API是一个非常有趣的事情。一个简单的方法是有一个PHP脚本上要求返回一些JSON数据的Web服务器。如果你想向发展这样的服务本教程可以帮助你,链接。
Developing a REST API is a very interesting thing. A easy way is to have a php script on a webserver that on request returns some json data. If you want to develope such a service this tutorial might help you, link.
这篇关于设计方法:Android和Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!