我正在开发一个应用程序,即使关闭该应用程序,我也需要24/7的用户位置。为了解决这个问题,我有一个AlarmManager,它每分钟执行代码以获取位置
public class LocationTracker extends BroadcastReceiver {
private int MY_PERMISSIONS_REQUEST_GET_LOCATION;
private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
private FusedLocationProviderClient fusedLocationClient;
//private LocationManager locationManager = (LocationManager) thi;
private LocationListener mLocationListener;
private LocationManager locationManager;
public static final String ACTION_PROCESS_UPDATE = "com.example.alwaysafe.UPDATE_LOCATION";
@Override
public void onReceive(Context context, Intent intent) {
locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10f, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("LISTENER", ".");
if (location != null) {
Log.i("LATITUDE LISTENER", String.valueOf(location.getLatitude()));
Log.i("LONGITUDE LISTENER", String.valueOf(location.getLongitude()));
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
}
当应用程序在模拟器上运行时,此代码有效,但是当我尝试在真实的智能手机上运行该代码时,AlarmManager已激活,但无法获取位置
答案 0 :(得分:0)
尝试更改requestLocationUpdates()
的两个参数中的这两个
首先,您可以尝试将10米的距离设置为0,以摆脱此标准。