Java 在没有活动的情况下运行IntentService

Java 在没有活动的情况下运行IntentService,java,android-studio,android-intent,background-process,Java,Android Studio,Android Intent,Background Process,我有一个需要在后台永久运行的服务,但一旦活动关闭,它创建的IntentService也会立即运行: public class CAS extends IntentService { public CAS() { super("CAS"); } Sensor accelerometer; SensorManager sensorManager; CA cA= new CA(); @Override protected void onHandleIntent(

我有一个需要在后台永久运行的服务,但一旦活动关闭,它创建的IntentService也会立即运行:

public class CAS extends IntentService {
  public CAS() {
    super("CAS");
  }

  Sensor accelerometer;
  SensorManager sensorManager;
  CA cA= new CA();

  @Override
  protected void onHandleIntent(@Nullable Intent intent) {
    sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    sensorManager.registerListener(cA, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);

    LocationManager locationManager = GlobalFunctions.LocationLibrary.getLocationManager(this);

    cA.start(this, locationManager);
  }
}

我怎样才能让这个类在后台无限期地运行,甚至在Android启动时也能运行呢?

您需要向BroadcastReceiver注册该服务

这里有一个同样的程序

检查设备重新启动时重新启动服务的应答

广播接收机

希望这有帮助