I am develop a app in Android. I use Location and Location manager to get a current position, but if i turn off and turn on the GPS on app execution, location.getLastKnownLocation
return null. This is the code.
@Override
public void onClick(View view) {
if(!checkGPSEnabled()){
dialogCheckGPS();
}else{
Intent i =new Intent(getApplicationContext(), ZoneUser.class);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location==null)
Toasty.success(getApplicationContext(),"Es null").show();
Bundle bundle = new Bundle();
bundle.putDouble("lat",location.getLatitude());
bundle.putDouble("lng",location.getLongitude());
i.putExtras(bundle);
startActivity(i);
}
}
I use Intent
and Bundle
to get latitude and longitude to another activity, but location return null.
Thanks.