After implement the current location http://androidbasedapplication.blogspot.in/2014/09/how-to-add-location-in-your-application.html then we can retrive the updates about the location. You can get the updates about your location with in periodic time slot.this app automatically update your location.last location update by
LocationClient.getLastLocation().
This location based on the currently available on wifi and GPS. The accuracy and frequency of updates are affected by location permissions tat you have requested and parameter you pass to location services with the request.Set update:
For update the location of the client you must check the availability of google play services. for implement of the services refer this link http://androidbasedapplication.blogspot.in/2014/09/how-to-add-location-in-your-application.html . after find out the availability then define the location update callback.
public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { ... // Define the callback method that receives location updates @Override public void onLocationChanged(Location location) { // Report to the UI that the location was updated String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } ... }
for define the update callback onlocationchange() method used.the location object contain the client's longitude and latitude value then it used locationlistener interface used. Now your callback is prepared and you can set up the request for the location updates.before the starting the location update before then here specify some update parameter below
Update parameters:
Here we want update of the location is create in some regular interval so lets define the interval parameter.
Update interval :
This parameter set by locationrequest.setinterval(). This method set the millisecond interval time which your application prefer the receive the location updates.
Fastest interval update:
This parameter set by locationrequest.setfastestinterval() and set the millisecond interval time. You just need to set this rate because other rate also affect the rate at which update are sent.If this rate is faster then you app can handle, you may encounter problems with UI flicker or data overflow. To prevent this call, LocationRequest.setFastestInterval() to set an upper limit to the upper rate.
public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { ... // Global constants ... // Milliseconds per second private static final int MILLISECONDS_PER_SECOND = 1000; // Update frequency in seconds public static final int UPDATE_INTERVAL_IN_SECONDS = 5; // Update frequency in milliseconds private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; // The fastest update frequency, in seconds private static final int FASTEST_INTERVAL_IN_SECONDS = 1; // A fast frequency ceiling in milliseconds private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; ... // Define an object that holds accuracy and frequency parameters LocationRequest mLocationRequest; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the LocationRequest object mLocationRequest = LocationRequest.create(); // Use high accuracy mLocationRequest.setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY); // Set the update interval to 5 seconds mLocationRequest.setInterval(UPDATE_INTERVAL); // Set the fastest update interval to 1 second mLocationRequest.setFastestInterval(FASTEST_INTERVAL); ... } ... }
Start update location :
For get the location update you need to create the client's location into the oncreate() method. Then connect it and calling the requestlocationupdate(). when client connect to the their app automatically client's location goes to in on start() method. for put the update request you need to call it in ConnectionCallbacks.onConnected() methods. If user wants to connect the location update with any reason then they can done easily in apps
SharedPreferences
in onPause(). it can be start by onResume()
.method.here below the coding display for the how to client's app goes into on create method and changes in to on start method.public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { ... // Global variables ... LocationClient mLocationClient; boolean mUpdatesRequested; ... @Override protected void onCreate(Bundle savedInstanceState) { ... // Open the shared preferences mPrefs = getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE); // Get a SharedPreferences editor mEditor = mPrefs.edit(); /* * Create a new location client, using the enclosing class to * handle callbacks. */ mLocationClient = new LocationClient(this, this, this); // Start with updates turned off mUpdatesRequested = false; ... } ... @Override protected void onPause() { // Save the current setting for updates mEditor.putBoolean("KEY_UPDATES_ON", mUpdatesRequested); mEditor.commit(); super.onPause(); } ... @Override protected void onStart() { ... mLocationClient.connect(); } ... @Override protected void onResume() { /* * Get any previous setting for location updates * Gets "false" if an error occurs */ if (mPrefs.contains("KEY_UPDATES_ON")) { mUpdatesRequested = mPrefs.getBoolean("KEY_UPDATES_ON", false); // Otherwise, turn off location updates } else { mEditor.putBoolean("KEY_UPDATES_ON", false); mEditor.commit(); } } ... /* * Called by Location Services when the request to connect the * client finishes successfully. At this point, you can * request the current location or start periodic updates */ @Override public void onConnected(Bundle dataBundle) { // Display the connection status Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); // If already requested, start periodic updates if (mUpdatesRequested) { mLocationClient.requestLocationUpdates(mLocationRequest, this); } } ... }
Stop update location:
User wants to turn off location updates then the state of update flag save in to the
onPause()
. for make the the stop update in onstop() by calling removeLocationUpdates(LocationListener)
.public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { ... /* * Called when the Activity is no longer visible at all. * Stop updates and disconnect. */ @Override protected void onStop() { // If the client is connected if (mLocationClient.isConnected()) { /* * Remove location updates for a listener. * The current Activity is the listener, so * the argument is "this". */ removeLocationUpdates(this); } /* * After disconnect() is called, the client is * considered "dead". */ mLocationClient.disconnect(); super.onStop(); } ... }
In this post you have the basic structure of the how client connect to the on create get the update and turn off the update location.
No comments:
Post a Comment