Last active
January 23, 2016 12:28
-
-
Save kuldeepiitg/bc7d7b9f6b9aa9e859ca to your computer and use it in GitHub Desktop.
Location activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.co.palup.android; | |
import android.Manifest; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.provider.Settings; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.NavigationView; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Toast; | |
import com.google.android.gms.common.ConnectionResult; | |
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; | |
import java.io.IOException; | |
/** | |
* Home activity | |
*/ | |
public class MainActivity extends AppCompatActivity | |
implements NavigationView.OnNavigationItemSelectedListener, | |
GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, | |
LocationListener { | |
private static final int PERMISSION_REQUEST_CODE_LOCATION = 1; | |
/** | |
* Http connection timeout | |
*/ | |
private final int TIME_OUT = 60 * 1000; | |
/** | |
* Tag for logger. | |
*/ | |
private final String TAG = "Home"; | |
/** | |
* Dialog to show when loading is going in background | |
*/ | |
private ProgressDialog progressDialog; | |
/** | |
* Google API client. | |
*/ | |
private GoogleApiClient googleApiClient; | |
/** | |
* Location request. | |
*/ | |
private LocationRequest locationRequest; | |
/** | |
* The location manager | |
*/ | |
private LocationManager locationManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_palup_home); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
if (googleApiClient == null) { | |
googleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
locationManager = | |
(android.location.LocationManager) this.getSystemService(LOCATION_SERVICE); | |
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { | |
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
this.startActivity(intent); | |
} | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != | |
PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, | |
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
PERMISSION_REQUEST_CODE_LOCATION); | |
} | |
} | |
protected void createLocationRequest() { | |
locationRequest = new LocationRequest(); | |
locationRequest.setInterval(10000); | |
locationRequest.setFastestInterval(5000); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
} | |
protected void startLocationUpdates() { | |
if (googleApiClient == null) { | |
Log.i(TAG, "client is null"); | |
} | |
if (locationRequest == null) { | |
Log.i(TAG, "location request is null"); | |
} | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
googleApiClient, locationRequest, this); | |
} | |
@Override | |
protected void onStart() { | |
googleApiClient.connect(); | |
super.onStart(); | |
} | |
@Override | |
protected void onStop() { | |
googleApiClient.disconnect(); | |
super.onStop(); | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == | |
PackageManager.PERMISSION_GRANTED && locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { | |
createLocationRequest(); | |
startLocationUpdates(); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.i("onLocationChanged", location.toString()); | |
// TODO: take action on location | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment