【发布时间】:2018-04-07 06:49:30
【问题描述】:
我有一个服务类,它将从上午 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);
}
};
但我无法在同一服务类中停止定位功能 下面是我的定位功能
定位功能:
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 部分工作,但定位功能仍在工作并且没有停止。
谁能解释一下是什么问题。 提前致谢。
【问题讨论】:
标签: android locationlistener google-location-services