Design Approach: Android and Web Service

I am developing an application for Android mobile phones that communicate with the json / rest web service. I need to periodically make certain calls to the server in order to check some information. In this context, I may also need to request a GPS for the current position. I don’t know at all how to use the local service, since I don’t know very well how to deal with them, in fact I need to periodically receive this data and update MapView accordingly. I heard that I can use PendingIntents in the service, bind this data as a payload and send it to the broadcast receiver, which decompresses the data and updates the user interface, I also heard that this is a bad design approach due to which broadcast receiver intended for use. Does anyone have any helpful hints?

+4
source share
1 answer

you need to deal with google maps first, as you will display the map. Look at this Using Google Maps in Android on mobiForge .

Secondly, you need a class that provides gps data. It’s easier to get location data and update the interface using a 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, instantiate this class and placelistener will start listening for gps updates. But you cannot access lng and lat, since you do not know from your activity, since they are set or zero. So you need a handler that sends a message to your main action when lat and lng are set:

Change 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 core business, 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 map, you can easily update your interface in the handler itself. Whenever gps data is available, a message is triggered and accepted by the handler.

Developing a REST API is a very interesting thing. An easy way is to have a php script on a web server that returns some json data on request. If you want to develop such a service, this tutorial can help you, link .

+2
source

All Articles