Android进程在杀死应用时遇到错误

时间:2018-02-08 07:12:09

标签: android android-service android-sensors

这是确切的错误

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

以下是该方案 它基本上是一个步骤跟踪器

  实施

StepTrackerShakeDetectorService ,以便在使用START_STICKY销毁应用时自动重启

但是当应用程序从任务列表中删除时,我收到此错误。

这是代码。

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);
    }


}

以下是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 

2 个答案:

答案 0 :(得分:0)

将此行放在)

中函数的末尾
onTaskRemoved

答案 1 :(得分:0)

更改返回null的onbind方法

@Override
public IBinder onBind(Intent intent) {
    return new Binder();
}
如果不能正常工作,请再次运行,请参阅以下链接

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

在下面的链接中,一个使用相同的程序来获取警报服务,其他服务在后台也可以检查

Service crashing and restarting