使用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
}
}
谢谢!
答案 0 :(得分:0)
改为使用 JobIntentService (在Kotlin中):
class GeofenceTransitionsIntentService : JobIntentService() {
override fun onHandleWork(intent: Intent) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
// ...
}
}