我有一个需要在后台永久运行的服务,但活动关闭后,它创建的 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运行时启动?
答案 0 :(得分:1)
您需要向BroadcastReceiver注册服务
这里是sample程序。
检查this答案以重启设备时重新启动服务
Documentation of BroadcastReceiver
希望这会有所帮助。