简体   繁体   中英

why can't I call wakeLock.release(); from a handler?

I am trying to give my locationListener some time to update, because it just doesn't have time to update if I call my quit() method right away. To combat this I thought I would just put my quit method in a handler to wait a little bit. Here is my quit() method:

private void quit(){
    mSensorManager.unregisterListener(MyService.this);
    wakeLock.release();
    this.stopSelf();
}

This methods purpose is to unregister my accelerometer, release my wakeLock, and then stop the service that has been started.

This is my stack trace:

07-28 11:50:44.023 25510-25510/com.example.cnash.raf E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.example.cnash.raf, PID: 25510
                                                                   java.lang.RuntimeException: WakeLock under-locked MyWakeLock
                                                                       at android.os.PowerManager$WakeLock.release(PowerManager.java:2016)
                                                                       at android.os.PowerManager$WakeLock.release(PowerManager.java:1986)
                                                                       at com.example.cnash.raf.MyService.quit(MyService.java:104)
                                                                       at com.example.cnash.raf.MyService.access$000(MyService.java:27)
                                                                       at com.example.cnash.raf.MyService$1.run(MyService.java:92)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

I have seen many people who get this same error have trouble because they don't acquire the wakeLock, but I aquire it during the onCreate of my service, like so:

@Override
public void onCreate(){
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

    PowerManager mgr = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    try {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, .1f, locationListener);
    } catch(SecurityException | IllegalArgumentException e) {
        e.printStackTrace();
    }

    Toast.makeText(this, "onCreate Successful", Toast.LENGTH_SHORT).show();
}

Below is where the problem is. I have an onSensorChanged that I want to record the accelerometer at that instant, and then also the position, and then quit. But the location just isn't getting updated fast enough. here is my onSensorChanged method:

@Override
public void onSensorChanged(SensorEvent event) {
    addingString = serializeDataIn();
    addingString += '\n' + "X, Y, Z: " + event.values[0] + ", " +
            event.values[1] + ", " + event.values[2];

    addingString += '\n' + "    " + "Longitude, Latitude: " + longitude + ", " + latitude;
    serializeDataOut(addingString);

    mHandler.postDelayed(new Runnable() {
        public void run(){
            quit();
        }
    }, 1000);
}

I want to add data for one second, and then quit. If I take out the handler and just call quit(); then my application runs smoothly. If I put it into a handler, it crashes. Why?

Please check if you are not releasing wake lock twice. To guard against this check if it is held before releasing.

if (wakeLock.isHeld()) {
    wakeLock.release();
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM