[英]Android foreground service with location updates gets killed once onHandle completed
i want to show persistence notification my location traction service but the service gets killed once the onHandleIndent completes, there is a timertask to get the location periodically , 我想向我的位置跟踪服务显示持久性通知,但是一旦onHandleIndent完成,该服务将被终止,有一个timertask定期获取该位置,
my problem is, Its working fine if onStartCommand not returning START_STICKY. 我的问题是,如果onStartCommand不返回START_STICKY,它的工作正常。 but in this case notification is not appearing.
但在这种情况下,不会显示通知。
i want my service to be running forever with persistence notification. 我希望我的服务通过持久性通知永远运行。
here is my class 这是我的课
public class TrackLocation extends IntentService {
public static final String START_TRACKING = "START_TRACKING";
public static final String STOP_TRACKING = "STOP_TRACKING";
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000;
double totalDistance = 0;
Location locationFirstOld;
protected LocationManager locationManager;
static double n = 0;
Long s1, r1;
double plat, plon, clat, clon, dis;
Thread t1;
EditText e1;
boolean bool = true;
private String booking_id;
private FusedLocationProviderClient mFusedLocationClient;
public static boolean isTrackingEnabled;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
*/
public TrackLocation() {
super("Distance tracking service");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
String action = intent.getAction();
if (action.equals(START_TRACKING)) {
isTrackingEnabled = true;
booking_id = intent.getStringExtra("booking_id");
startTracking();
} else {
isTrackingEnabled = false;
}
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
return START_STICKY;
}
private void startTracking() {
final Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (isTrackingEnabled) {
Log.d("tracking", "run: " + new Date().toString());
getLocation();
} else {
timer.cancel();
}
}
};
timer.schedule(timerTask,0,5000);
Intent notificationIntent = new Intent(this, PickaupActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
notificationIntent, 0);
Notification notification = new Notification.Builder(this)
.setContentTitle("On going trip")
.setContentText("Active trips")
.setSmallIcon(R.drawable.ic_notification_auto_request)
.setContentIntent(pendingIntent)
.setTicker("Trip started")
.build();
startForeground(1337, notification);
}
private void getLocation() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mFusedLocationClient.getLastLocation().addOnSuccessListener( new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Log.d("", "onSuccess: " + location.getLatitude());
submitData(location);
}
}
});
}
private void submitData(Location location) {
if (locationFirstOld == null) {
locationFirstOld = location;
return;
}
// sendMyLocation(location);
// double distance = getDistance(locationFirstOld.getLatitude(), locationFirstOld.getLongitude(), location.getLatitude(), location.getLongitude());
double distance = locationFirstOld.distanceTo(location);
Log.d("distance", "submitData: " + String.valueOf(distance));
locationFirstOld = location;
sendMessageToActivity(totalDistance);
totalDistance += distance;
}
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}
private void sendMessageToActivity(double distance) {
Intent intent = new Intent("DISTANCE_UPDATE");
// You can also include some extra data.
intent.putExtra("distance", distance);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void sendMyLocation(Location location) {
Intent intent = new Intent();
intent.putExtra("lat", location.getLatitude());
intent.putExtra("lat", location.getLongitude());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
the service gets killed once the onHandleIndent completes
一旦onHandleIndent完成,服务将被终止
That is standard behavior for IntentService
— IntentService
calls stopSelf()
when onHandleIntent()
returns, if there are no more queued-up commands. 这是标准行为
IntentService
- IntentService
调用stopSelf()
时onHandleIntent()
返回时,如果没有更多的排队等候命令。
Do not use IntentService
for this scenario. 在这种情况下不要使用
IntentService
。 Create you own service with your own threading model, calling stopSelf()
when you no longer need the service. 使用自己的线程模型创建自己的服务,在不再需要该服务时调用
stopSelf()
。
IntentService stops when it is done doing whatever it does. IntentService在完成任何操作后都会停止。
Use Service instead, Service types are not stopped until the system, user or the app stops it 改为使用服务,直到系统,用户或应用停止服务类型后,服务类型才会停止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.