使用Oreo时,在onHandleIntent()中处理地理围栏转换事件时,我们需要立即启动Foreground服务,还是可以在onHandleIntent()函数中处理所需的工作(我的理解是在后台运行,但不应在Oreo中允许):

public class GeofenceTransitionsIntentService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
      //Do work here...
    }
}




public class GeofenceTransitionsIntentService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
        Intent startIntent = new Intent(this, ForegroundServices.class);
        this.startForegroundService(startIntent); //and do needed work
        //inside the Foreground service
    }
}


谢谢!

最佳答案

使用JobIntentService代替(在Kotlin中):

class GeofenceTransitionsIntentService : JobIntentService() {
    override fun onHandleWork(intent: Intent) {
        val geofencingEvent = GeofencingEvent.fromIntent(intent)
        // ...
    }
}

10-08 09:01