Java – do not stop the Android service when stopservice() is called

I have a problem stopping the service I have started

The service will be called when the user logs in and starts tracking the user's location. This works normally. When the user presses the logout button and logs out successfully, the service will stop

This is an Android service, which is called through HTML button and JavaScript interface

This is my main class, which contains methods to start and stop services:

public class CasesMain extends DroidGap {
Intent gpsTracking;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();

    appView.addJavascriptInterface(this, "StartGPS");

    super.loadUrl(Config.getStartUrl());
}
public void startGPS(){
    gpsTracking = new Intent(this, MyService.class);
    startService(gpsTracking);
}

public void stopGPS(){
    stopService( gpsTracking );
}

}

This is the myservice class:

public class MyService extends Service {
private LocationManager locManager;
private LocationListener locListener = new myLocationListener();

private boolean gps_enabled = false;
private boolean network_enabled = false;

private Handler handler = new Handler();
Thread t;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
}

@Override
public void onDestroy() {
}

@Override
public void onStart(Intent intent, int startid) {
}

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

    Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
            .show();

    final Runnable r = new Runnable() {
        public void run() {
            location();
            handler.postDelayed(this, 10000);
        }
    };
    handler.postDelayed(r, 10000);
    return START_STICKY;
}

public void location() {
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    try {
        gps_enabled = locManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }
    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locListener);
    }
    if (network_enabled) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                0, 0, locListener);
    }
}

private class myLocationListener implements LocationListener {
    double lat_old = 0.0;
    double lon_old = 0.0;
    double lat_new;
    double lon_new;

    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
            locManager.removeUpdates(locListener);
            lon_new = location.getLongitude();
            lat_new = location.getLatitude();
            String longitude = "Longitude: " + location.getLongitude();
            String latitude = "Latitude: " + location.getLatitude();
            Log.v("Debug",  "Latt: " + latitude + " Long: " + longitude);
            Toast.makeText(getApplicationContext(),
                    longitude + "\n" + latitude, Toast.LENGTH_SHORT).show();
            lat_old = lat_new;
            lon_old = lon_new;
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

}

This is my call to stop the service: window. Startgps. Stopgps();

It works perfectly at first, but when I exit, the application continues to display characters and long messages, which means that the call to stopservice () either doesn't work or is not called

Anyone can see where my mistake is or what's wrong?

Any help will be appreciated!

Add handler.removecallbacksandmessages (null) fixedly; Fixed it for me in the ondestroy () method of myservice

resolvent:

You have an empty ondestroy () method. You need to call removecallbacks () on the handler there

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>