Error closing a service in java on android

0

Hello! I'm having an error closing my app's service:

03-13 13:34:13.572: E/test(9039): Exception
03-13 13:34:13.582: E/AndroidRuntime(9039): FATAL EXCEPTION: main
03-13 13:34:13.582: E/AndroidRuntime(9039): Process: com.emersonbarcellos.mototaxi, PID: 9039
03-13 13:34:13.582: E/AndroidRuntime(9039): java.lang.RuntimeException: Unable to stop service com.emersonbarcellos.mototaxi.GpsService@41ab6c48: java.lang.RuntimeException: WakeLock under-locked DoNotSleep
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.app.ActivityThread.handleStopService(ActivityThread.java:2844)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.app.ActivityThread.access$2200(ActivityThread.java:145)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.os.Looper.loop(Looper.java:136)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.app.ActivityThread.main(ActivityThread.java:5214)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at java.lang.reflect.Method.invoke(Method.java:515)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:814)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at dalvik.system.NativeStart.main(Native Method)
03-13 13:34:13.582: E/AndroidRuntime(9039): Caused by: java.lang.RuntimeException: WakeLock under-locked DoNotSleep
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.os.PowerManager$WakeLock.release(PowerManager.java:764)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.os.PowerManager$WakeLock.release(PowerManager.java:735)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at    com.emersonbarcellos.mototaxi.GpsService.onDestroy(GpsService.java:152)
03-13 13:34:13.582: E/AndroidRuntime(9039):     at android.app.ActivityThread.handleStopService(ActivityThread.java:2827)

Service:

public class GpsService extends Service {

WakeLock wakeLock;
String chamado = null;
private LocationManager locationManager;
SharedPreferences sharedPrefs;

public GpsService() {
    // TODO Auto-generated constructor stub
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

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

    PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);

    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

    // Toast.makeText(getApplicationContext(), "Service Created",
    // Toast.LENGTH_SHORT).show();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    Log.e("Google", "Service Created");

}

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

    // Bundle b = intent.getExtras();
    // chamado = b.get("ID_CHAMADO").toString();
    return startId;
}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    Log.e("Google", "Service Started");

    locationManager = (LocationManager) getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            10000, 0, listener);

}

private LocationListener listener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        Log.e("Google", "Location Changed");
        String lat = String.valueOf(location.getLatitude());
        String lon = String.valueOf(location.getLongitude());
        tentativa(lat, lon);

    }

    private void tentativa(String lat, String lon) {

        final HttpPost Online = new HttpPost(
                "http://fabianrepresentacoes.com.br/gcm/server.php?tipo=update_cordenadas&id="
                        + sharedPrefs.getString("idChamAndamento", "NULL")
                        + "&latitude=" + lat + "&longitude=" + lon);

        final HttpClient client = new DefaultHttpClient();
        Log.v("aviso",
                "http://fabianrepresentacoes.com.br/gcm/server.php?tipo=update_cordenadas&id="
                        + sharedPrefs.getString("idChamAndamento", "NULL")
                        + "&latitude=" + lat + "&longitude=" + lon);
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    HttpResponse responsePOST = client.execute(Online);
                    String responseBody = EntityUtils.toString(responsePOST
                            .getEntity());

                    Log.v("aviso", "Resposta " + responseBody);

                } catch (Exception e) {
                    // TODO: handle exception
                    Log.i("aviso", "erro ao enivar");
                    // tentativa();
                }

            }
        }).start();

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
};

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

    wakeLock.release();
    // Log.e("Google", "Service Destroy");
}

public static boolean isConnectingToInternet(Context _context) {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }

    }
    return false;
}

 }

What will it be?

    
asked by anonymous 13.03.2015 / 17:46

1 answer

1

Try reversing the rows

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

    wakeLock.release();
    // Log.e("Google", "Service Destroy");
}

for

@Override
public void onDestroy() {
   wakeLock.release();
    // Log.e("Google", "Service Destroy");

   // TODO Auto-generated method stub
    super.onDestroy();
}
    
14.03.2015 / 01:07