Is it possible to prevent Service(startForground) from being killed by

os?

不,这是Android决定哪个进程必须停止的时候.唯一的方法是通过减少消耗的内存量使其“不再被吸引”.

节省内存.将服务放入自己非常小的过程中.所以android os可以杀死主进程来回收内存,你的服务可以继续运行.通过传入用于启动服务的意图的设置和首选项来维护一个很小的服务代码.

注意:小服务必须是前台服务,这意味着它必须在状态栏中显示一个图标.

表明单独的过程

android:name="com.gosylvester.bestrides.ServiceLocationRecorder"

android:process=":bestRidesService" >

传递具有用于启动服务的意图的设置.只需重新启动服务即可更改设置.

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

boolean isTrackerMarker = SettingMarker.TRACKER_MARKER_DEFAULT;

if (intent != null) {

// intent is processing = b

isMiles = intent.getBooleanExtra(

SettingApplication.APPLICATION_MILE,

SettingApplication.APPLICATION_MILE_DEFAULT);

isRecordAccuracy = intent.getBooleanExtra(

SettingRecord.RECORD_ACCURACY,

SettingRecord.RECORD_ACCURACY_DEFAULT);

locationInterval = intent.getLongExtra(SettingRecord.RECORD_MIlLIS,

SettingRecord.RECORD_PRESET_MEDIUM_MILLIS);

startMillis = intent.getLongExtra(BUNDLE_START_MILLIS, 0);

distance = intent.getDoubleExtra(

ServiceLocationRecorder.BUNDLE_TRACKED_DISTANCE, 0);

recordDistance = (float) intent.getIntExtra(

SettingRecord.RECORD_DISTANCE,

SettingRecord.RECORD_PRESET_MEDIUM_DISTANCE);

boolean newIsRecording = intent.getBooleanExtra(

SettingRecord.RECORDING, isRecording);

isTrackerMarker = intent.getBooleanExtra(

SettingMarker.TRACKER_MARKER,

SettingMarker.TRACKER_MARKER_DEFAULT);

startRecording(newIsRecording);

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐