Android process is bad error on killing the app

2020-07-19 06:18发布

问题:

Here is the exact error

02-08 12:36:43.490 3479-4980/? W/ActivityManager: Scheduling restart of crashed service com.wfl/.StepTrackerShakeDetectorService in 1000ms

02-08 12:36:44.494 3479-3513/? W/ActivityManager: Unable to launch app com.wfl/10139 for service Intent { cmp=com.wfl/.StepTrackerShakeDetectorService }: process is bad

Here is the scenario It is basically a step tracker

StepTrackerShakeDetectorService is implemented to restart automatically when app is destroyed using START_STICKY

But when the app is removed from task list I am getting this error.

Here is the code.

public class StepTrackerShakeDetectorService extends Service {

    private SensorManager mSensorManager;
    private StepTrackerShakeDetector mShakeDetector;
    private Sensor step_counter_sensor;
    private Sensor step_detector_sensor;
    private Sensor step_accelerometer;

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

    @Override
    public void onCreate() {

        registerDetector();
    }


    private void registerDetector() {

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        step_counter_sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        step_detector_sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        step_accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if (step_counter_sensor != null) // sensor not supported
        {
            mShakeDetector = new StepTrackerShakeDetector(Sensor.TYPE_STEP_COUNTER);
            mSensorManager.registerListener(mShakeDetector, step_counter_sensor, SensorManager.SENSOR_DELAY_FASTEST);
        } else if (step_accelerometer != null) {

            mShakeDetector = new StepTrackerShakeDetector(Sensor.TYPE_ACCELEROMETER);
            mSensorManager.registerListener(mShakeDetector, step_accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
        }


        mShakeDetector.setOnShakeListener(new StepTrackerShakeDetector.OnShakeListener() {

            @Override
            public void onShake(int count) {
              //Code to calculate steps
            }
        });
    }

    private void unregisterDetector() {
        mSensorManager.unregisterListener(mShakeDetector);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        unregisterDetector();
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Intent intent = new Intent(getApplicationContext(), StepTrackerShakeDetectorService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 2000, pendingIntent);
    }


}

Here is the error in lenovo phab 2

02-13 11:42:12.211 975-1964/? W/ActivityManager: Scheduling restart of crashed service com.wfl/.StepTrackerShakeDetectorService in 1000ms
02-13 11:42:12.213 975-1964/? I/ActivityManager:   Force stopping service ServiceRecord{7a45ff2 u0 com.wfl/.StepTrackerShakeDetectorService}
02-13 11:42:12.214 975-1964/? V/ActivityManager: Broadcast: Intent { act=android.intent.action.PACKAGE_RESTARTED dat=package:com.wfl flg=0x10 (has extras) } ordered=false userid=0 callerApp=null
02-13 11:42:12.216 975-2003/? W/ActivityManager: Spurious death for ProcessRecord{2325a63 0:com.wfl/u0a146}, curProc for 30626: null
02-13 11:42:12.790 2342-2361/? D/GasService: FG app changed: from com.wfl to 

回答1:

Put this line at the end of the function in onTaskRemoved

        super.onTaskRemoved(rootIntent);


回答2:

Change your onbind method which returns null to

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

and run again if not working please see the below links

Why does my Android service get restarted when the process is killed, even though I used START_NOT_STICKY?

In the below link, one is using the same procedure to get alarm services and the others services in back ground check this out also

Service crashing and restarting