【问题标题】:Run an IntentService without an Activity在没有 Activity 的情况下运行 IntentService
【发布时间】:2018-12-21 21:27:56
【问题描述】:

我有一个需要在后台永久运行的服务,但是一旦活动关闭,它创建的 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 运行时启动?

【问题讨论】:

    标签: java android-studio android-intent background-process


    【解决方案1】:

    您需要使用 BroadcastReceiver 注册服务

    这是一个sample 程序。

    在设备重启时检查this 重启服务的答案

    Documentation 的 BroadcastReceiver

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2018-06-06
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多