Sunday, April 17, 2016

Location Based Services Part-II (Get Updates in every 30 seconds)

Please refer Part - I

Location Based Services Part-I (Find Latitude & Longitude)


And make few changes in MainActivity.java file.
Please see.....

A. One more method added and LocationListener added   in 
       MainActivity.java   is as 

package com.exam.ravi.locationbsex1;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity
         implements GoogleApiClient.OnConnectionFailedListener ,
                GoogleApiClient.ConnectionCallbacks ,LocationListener
{
    TextView dis;
    GoogleApiClient googleApiClient;
    int SERVICE_CODE = 99;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dis = (TextView) findViewById(R.id.textLocation);
        if(checkServices())
        {
            clientInitialization();
        }
    }
    private void clientInitialization() {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addOnConnectionFailedListener(this)
                .addConnectionCallbacks(this)
                .build();
    }
    @Override    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
    }

    @Override    protected void onStop() {
        super.onStop();
        if(googleApiClient.isConnected())
            googleApiClient.disconnect();
    }

    private boolean checkServices()
    {
        int result= GooglePlayServicesUtil.isGooglePlayServicesAvailable
                                             (getApplicationContext());
        if(result!= ConnectionResult.SUCCESS)
        {
            if(GooglePlayServicesUtil.isUserRecoverableError(result))
            {
             GooglePlayServicesUtil.getErrorDialog(result,this,SERVICE_CODE).show();
            }
            return false;
        }
        return true;
    }

    @Override    public void onConnected(Bundle bundle) {
         LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30000);
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,
                                                        locationRequest,this);
    }

    @Override    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Connection Susspended", Toast.LENGTH_SHORT).show();
    }

    @Override    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show();

    }

    @Override    public void onLocationChanged(Location location) {
        
        if(location!=null)
            dis.setText("Latitude = " + location.getLatitude()+ 
                              "Longitude" + location.getLongitude());
        else            dis.setText("Location Not Found");


    }
}

No comments:

Post a Comment