• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to avoid crashes and update time in this gps-application?

My goal is to get the gps-location continuously about every second. This is what I tried, I stripped it down to the GPS-part. Not only does it not get the GPS-location every second, it also crashes after some time, meaning, that no new position is being fetched anymore. Also it seems to be a huge burden on the phone performancewise.

Java:
package com.me.bluetoothtest;


public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_ACCESS_COARSE_LOCATION = 0;
private static final int PERMISSION_ACCESS_FINE_LOCATION = 2;
private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 3;

LocationManager mLocationManager;
LocationListener mLocLi;

@Override
protected void onCreate(Bundle savedInstanceState) {

  mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

  genehmigen(android.Manifest.permission.ACCESS_COARSE_LOCATION, PERMISSION_ACCESS_COARSE_LOCATION);
  genehmigen(android.Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_ACCESS_FINE_LOCATION);
  genehmigen(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_WRITE_EXTERNAL_STORAGE);



  if (mLocLi == null) {
   mLocLi = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {

     aktuell = location;
     Log.v("Bluetooth", aktuell.getLatitude() + " " + aktuell.getLongitude());

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
   };

   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
     2, mLocLi);
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
     2, mLocLi);
   }
  }

}



private void genehmigen(String Erlaubnis, int AnfrageCode) {
  if (ActivityCompat.checkSelfPermission(this, Erlaubnis) != PackageManager.PERMISSION_GRANTED) {
   ActivityCompat.requestPermissions(this, new String[] {
     Erlaubnis
    },
    AnfrageCode);
  }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

  if (requestCode == PERMISSION_ACCESS_COARSE_LOCATION || requestCode == PERMISSION_ACCESS_FINE_LOCATION) {
   if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    mLocLi = new LocationListener() {
     @Override
     public void onLocationChanged(Location location) {
      aktuell = location;
      Log.v("Bluetooth", aktuell.getLatitude() + " " + aktuell.getLongitude());
     }

     @Override
     public void onStatusChanged(String provider, int status, Bundle extras) {}

     @Override
     public void onProviderEnabled(String provider) {}

     @Override
     public void onProviderDisabled(String provider) {}
    };
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
     if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
       2, mLocLi);
     if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
      mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
       2, mLocLi);
    }
   } else {
    Toast.makeText(this, "Abgelehnt",
     Toast.LENGTH_LONG).show();
    //cancelOperation();
   }
  }
}


@Override
protected void onPause() {
  super.onPause();
  try {

   mBluetoothAdapter.cancelDiscovery();
   if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[] {
      android.Manifest.permission.ACCESS_COARSE_LOCATION
     },
     1000);
   }
   if (this != null) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

     ActivityCompat.requestPermissions(this, new String[] {
       android.Manifest.permission.ACCESS_FINE_LOCATION
      },
      1000);
    }
   }
   if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[] {
      android.Manifest.permission.WRITE_EXTERNAL_STORAGE
     },
     1000);
   }
   mLocationManager.removeUpdates(mLocLi);
  } catch (Exception m) {

  }
}

@Override
protected void onDestroy() {
  super.onDestroy();
  if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

   ActivityCompat.requestPermissions(this, new String[] {
     android.Manifest.permission.ACCESS_COARSE_LOCATION
    },
    1000);
  }
  if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

   ActivityCompat.requestPermissions(this, new String[] {
     android.Manifest.permission.ACCESS_FINE_LOCATION
    },
    1000);
  }
  if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

   ActivityCompat.requestPermissions(this, new String[] {
     android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    },
    1000);
  }
  mLocationManager.removeUpdates(mLocLi);
}

private class DownloadFilesTask extends AsyncTask < URL, Integer, Long > {
  protected Long doInBackground(URL...urls) {
   if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    Location l = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (l == null)
     l = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (l == null)
     return totalSize;
   }
   return totalSize;
  }
}
}


What is the reason? I presume some causes to be around
Java:
Location l = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
but I haven't found a working example for my purpose, yet.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones