Hola a todos
Para la construccion de un taximetro necesito asegurar que un Android Service que he construido se mantenga siempre activo mientras este encendido el celular, en mi caso he construido un servicio pero cuando abro un juego o una app como waze se finaliza el Service y el proceso que esta realizando dicho service se pierde (Toda la información relacionada a este proceso)
En mi android manifest configure el gps de esta manera
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
public class ServiceTaximeter extends Service{
........
private String getProviderName() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_HIGH);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(false);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
return locationManager.getBestProvider(criteria, true);
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
timeLocationListener = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MapActivity.mBroadcastDatos);
broadcastIntent.putExtra("Latitude", lat);
broadcastIntent.putExtra("Longitude", lng);
sendBroadcast(broadcastIntent);
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "In onStartCommand");
locationManager.requestLocationUpdates(getProviderName(), 5000, 0, timeLocationListener, Looper.getMainLooper());
play();
return START_NOT_STICKY;
}
private void play() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Titulo")
.setAutoCancel(false)
.setContentText("Ejecutandose");
Intent i=new Intent(this, MapActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi=PendingIntent.getActivity(this, 0,
i, 0);
mBuilder.setContentIntent(pi);
final Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1337, notification);
}
private void stop() {
stopForeground(true);
}
@Override
public IBinder onBind(Intent intent) {
return (null);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy() {
stop();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(timeLocationListener);
super.onDestroy();
}
Así inicio el Service :
Intent serviceIntent = new Intent(MapActivity.this, ServiceTaximeter.class);
startService(serviceIntent);
Me podrían indicar como puedo hacer para que nos e cierre este service, estuve averiguando y según algunas fuentes el uso de startForeground es el mas eficaz, por favor podrían indicarme si lo estoy aplicando bien o si hay una mejor forma de hacer esto, gracias por el apoyo.