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 ,
my problem is, Its working fine if onStartCommand not returning 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
That is standard behavior for IntentService
— IntentService
calls stopSelf()
when onHandleIntent()
returns, if there are no more queued-up commands.
Do not use IntentService
for this scenario. Create you own service with your own threading model, calling stopSelf()
when you no longer need the service.
IntentService stops when it is done doing whatever it does.
Use Service instead, Service types are not stopped until the system, user or the app stops it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.