Geofence Transition onHandleIntent是背景,但Oreo会允许吗?

时间:2018-01-03 20:26:57

标签: android android-8.0-oreo

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

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

VS

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
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

改为使用 JobIntentService (在Kotlin中):

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