我有一个服务类,它将从上午9:00到下午18:00获取用户的位置。我正在使用警报管理器来触发广播接收器中的selfStop();
,如
警报管理器:
public void StopTracking() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, StopTime);
calendar.set(Calendar.MINUTE, StopMin);
String stop = "stop";
registerReceiver(stopReceiver, new IntentFilter(stop));
if (PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT) != null) {
Log.i(TAG, "Pending Intent Started");
PendingIntent broadcastIntent = PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent);
} else {
Log.i(TAG, "StopTracking: Pending Intent Stoped!");
}
}
protected BroadcastReceiver stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received stop broadcast");
// Stop the service when the notification is tapped
unregisterReceiver(stopReceiver);
stopSelf();
intent = new Intent(context, StopService.class);
context.startService(intent);
}
};
但我无法在同一服务类中停止位置功能 bellow是我的位置功能
位置功能:
private void requestLocationUpdates() {
request.setInterval(10000);
request.setFastestInterval(5000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
final String path = getString(R.string.firebase_path) + "/" + getString(R.string.transport_id);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
// Request location updates and when an update is
// received, store the location in Firebase
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
ref.setValue(location);
Toast.makeText(TrackerService.this, "Success", Toast.LENGTH_SHORT).show();
String lattitude = String.valueOf(location.getLatitude());
String longitude = String.valueOf(location.getLongitude());
Save_LocationOffline(lattitude, longitude);
}
}
}, null);
}
}
这是我的问题,
广播接收器正常工作,我已触发selfStop();
和OnDestroy
部分工作,但位置功能仍然有效且未停止。
任何人都可以解释什么是问题。 在此先感谢。
答案 0 :(得分:1)
您似乎正在使用Google Play服务进行位置检索位置。
如果您查看FusedLocationProviderClient documentation,他们会说当您使用requestLocationUpdates(LocationRequest, LocationCallback, Looper)
时,请拨打removeLocationUpdates(LocationCallback)
。
首先需要提取位置回调,例如:
class MyService .... {
// Member into your service
private LocationCallback mLocationCallback = new LocationCallback() {...}
private void requestLocationUpdates() {
...
client.requestLocationUpdates(request, mLocationCallback, null)
...
}
// Into you stopReceiver
public void onReceive(Context context, Intent intent) {
...
client.removeLocationUpdate(mLocationCallback )
...
}
}