从扩展的Application类停止Android服务

时间:2014-02-22 15:26:02

标签: java android service

我遇到了一些麻烦,试图阻止我之前在Android应用程序类扩展的类的onCreate()上启动的服务。 GPS应该在应用程序执行期间运行(该部分工作正常)但是当应用程序关闭时,服务仍在运行。我试图使用onTerminate()因为onClose()在Applicaction类中不存在,并且服务一直在运行。

   public class ControlEntrega extends Application {

    public GPSBean posicion;
    public ServicioGPS servicio;

    public void onCreate() {
        Log.d(TAG,"onCreate");
        super.onCreate();
        usuario=null;
        cliente=null;
        entrega=null;
        dal=new DAL(getApplicationContext());
        crearDB();
        util=new Utilitario();
        servicio=new ServicioGPS();
        posicion=new GPSBean();
        iniciarGPS();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.d(TAG,"onTerminate");
        try {
            stopService(new Intent(this,ServicioGPS.class));
        } catch (Exception e) {
            Log.d(TAG,"onTerminate error: "+e.getMessage());
        }


    }
}

服务类:

public class ServicioGPS extends Service实现了LocationListener {

String TAG=ServicioGPS.class.getCanonicalName();
LocationManager locationManager=null;
public int tiempoGPS=1;
public int distanciaMetros=10;
Intent notificacion;
ControlEntrega controlador;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onDestroy() {

    super.onDestroy();
    Log.d(TAG,"onDestroy");
    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.removeUpdates(this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d(TAG, "onStartCommand");

    controlador= (ControlEntrega)getApplicationContext();
    if(controlador!=null){
        if(controlador.posicion!=null){
            controlador.posicion.imprimir();
        }
    }
    locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria,true);

    //LocationManager.GPS_PROVIDER
    //LocationManager.NETWORK_PROVIDER
    locationManager.requestLocationUpdates(provider,1000 * 60 * tiempoGPS,distanciaMetros,this);

    return super.onStartCommand(intent, flags, startId);
}


@Override
public void onLocationChanged(Location location) {

    Log.d(TAG, "onLocationChanged");

    //Seteando los valores del objeto location
    if(controlador!=null){
        Log.d(TAG, "seteando bean GPS");
        controlador.posicion=new GPSBean();
        controlador.posicion.latitud=location.getLatitude();
        controlador.posicion.longitud=location.getLongitude();
        controlador.posicion.altitud=location.getAltitude();
        controlador.posicion.precision=location.getAccuracy();
        controlador.posicion.proveedor=location.getProvider();
        controlador.posicion.tiempo=location.getTime();
        controlador.posicion.velocidad=location.getSpeed();
        controlador.posicion.imprimir();
    }

    //Notificación
    Intent notificacion = new Intent(getString(R.string.intentGPS));
    sendBroadcast(notificacion);

    //Se para asi mismo, ya se calendarizo antes
    stopSelf();

}

@Override
public void onProviderDisabled(String provider) {


}

@Override
public void onProviderEnabled(String provider) {


}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {


    }

  }

有一种方法我可以知道应用程序何时完全停止?所以我可以停止服务。

此致

阿尔弗雷

1 个答案:

答案 0 :(得分:0)

public class EyeCastApplication extends Application {

    static Context context;


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        context = getApplicationContext();

    }

    /***
     * start Heart Beat Loop
     */
public static void stopHeartBeatLoop() {
        context.stopService(new Intent(context, CmdConnectService.class));
    }



enter code here

    enter code here

}