我正在尝试使用Firebase
Cloud Messaging 向我的 Android 应用程序发送一个命令,提示它确定其当前的location
. 这是在课堂上完成的FCMService
。
然后该类SingleShotLocationProvider
通过location
使用FusedLocationProviderClient
. 然而,回调fusedTrackerCallback
永远不会被调用,尽管必要permissions
的被授予并被GPS
打开。为什么?
FCMService 类
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
?