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?
问题:
回答1:
first you have to deal with google maps since you will display a mapview. Have a look at this Using Google Maps in Android on mobiForge.
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);
}
}
}
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:
Modify in the following method:
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);
}
};
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.
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.