Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LocationManager locmngr; // Location manager declaration
- locationUpdate(); // Initializing the method in onCreate()
- public void locationUpdate()
- {
- locmngr = (LocationManager) getSystemService(LOCATION_SERVICE);
- if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 12);
- return;
- }
- boolean gps_enabled = false;
- boolean network_enabled = false;
- try {
- gps_enabled = locmngr.isProviderEnabled(LocationManager.GPS_PROVIDER);
- } catch (Exception ex) {
- Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
- }
- try {
- network_enabled = locmngr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- } catch (Exception ex) {
- Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
- }
- if (!gps_enabled && !network_enabled) {
- // notify user
- AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setMessage("GPS network not enabled");
- dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface paramDialogInterface, int paramInt) {
- // TODO Auto-generated method stub
- Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivity(myIntent);
- //get gps
- }
- });
- dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface paramDialogInterface, int paramInt) {
- // TODO Auto-generated method stub
- }
- });
- dialog.show();
- }
- try {
- locmngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 7, 30, new LocationListener() {
- @Override
- public void onLocationChanged(final Location location) {
- // Add a marker at your current location and move the camera
- //Log.e("location", location.toString());
- if(!user_id.isEmpty()){
- // code to write lat longs into firestore
- }
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- ActivityCompat.requestPermissions(TabsActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 12);
- }
- });
- } catch (Exception ex) {
- Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
- }
- }
- private final Runnable m_Runnable = new Runnable()
- {
- public void run()
- {
- try {
- locationUpdate();
- TabsActivity.this.mHandler.postDelayed(m_Runnable, 1000);
- }
- catch (Exception ex)
- {
- Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
- }
- }
- };
Add Comment
Please, Sign In to add comment