Я пытаюсь использовать Firebase
Cloud Messaging для отправки команды в мое приложение для Android, которая запрашивает у него определение текущего location
.Это делается в классе FCMService
.
. Затем класс SingleShotLocationProvider
выполняет фактическую работу, запрашивая location
обновления с использованием FusedLocationProviderClient
.Однако обратный вызов fusedTrackerCallback
никогда не вызывается, хотя необходимые permissions
предоставлены и GPS
включен.Почему?
Класс обслуживания FCMS
public class FCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("FCMService", "Message received");
if(remoteMessage.getData().containsKey("command") && remoteMessage.getData().get("command").equalsIgnoreCase("locate")) {
// get current location
SingleShotLocationProvider locProv = new SingleShotLocationProvider(getApplicationContext());
locProv.requestSingleUpdate();
System.out.println("Requested single location update.");
}
}
}
}
Класс SingleShotLocationProvider
public class SingleShotLocationProvider {
private Context context;
final LocationManager locManager;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback fusedTrackerCallback;
public SingleShotLocationProvider(Context context) {
this.context = context;
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
locManager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
if (locManager != null && !locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("SiSoLocProvider", "GPS IS NOT enabled.");
} else {
Log.d("SiSoLocProvider", "GPS is enabled.");
}
}
@TargetApi(16)
public void requestSingleUpdate() {
Looper.prepare();
// only works with SDK Version 23 or higher
if (android.os.Build.VERSION.SDK_INT >= 23) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// permission is not granted
Log.e("SiSoLocProvider", "Permission not granted.");
return;
} else {
Log.d("SiSoLocProvider", "Permission granted.");
}
} else {
Log.d("SiSoLocProvider", "SDK < 23, checking permissions should not be necessary");
}
final long startTime = System.currentTimeMillis();
fusedTrackerCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
if((locationResult.getLastLocation() != null) && (System.currentTimeMillis() <= startTime+30*1000)) {
System.out.println("LOCATION: " + locationResult.getLastLocation().getLatitude() + "|" + locationResult.getLastLocation().getLongitude());
System.out.println("ACCURACY: " + locationResult.getLastLocation().getAccuracy());
mFusedLocationClient.removeLocationUpdates(fusedTrackerCallback);
} else {
System.out.println("LastKnownNull? :: " + (locationResult.getLastLocation() == null));
System.out.println("Time over? :: " + (System.currentTimeMillis() > startTime+30*1000));
}
}
};
LocationRequest req = new LocationRequest();
req.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
req.setFastestInterval(2000);
req.setInterval(2000);
mFusedLocationClient.requestLocationUpdates(req, fusedTrackerCallback, null);
}
}
Вывод:
//Message received
//GPS is enabled.
//Permission granted.
//Requested single location update.
Но не более того.Зачем?Возможно, потому что permission
был предоставлен через service application context
?