gpt4 book ai didi

android - Android O 从 IntentService 迁移到 JobIntentService 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:35 24 4
gpt4 key购买 nike

我正在使用 Intent Service 来监控地理围栏转换。为此,我正在使用粘性服务的后续调用。

 LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
)

并且 Pending Intent 调用 Transition 服务(一个 IntentService),如下所示。

  private PendingIntent getGeofencePendingIntent() {
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the
//same pending intent back when calling addgeoFences()
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

Pre Oreo 效果很好。但是,我必须将粘性服务转换为 JobScheduler,并且我需要将 GeofenceTransitionsIntentService 转换为 JobIntentService。

话虽如此,我不确定如何返回为 JobIntentService 创建 PendingIntent,因为我需要为 JobIntentService 调用 enqueueWork。

任何建议/指针将不胜感激。

最佳答案

问题

在 Android Oreo+ 设备上从 IntentService 迁移到 JobIntentService 时,我遇到了同样的问题。

我发现的所有指南和 fragment 都不完整,它们遗漏了此迁移对使用 PendingIntent.getServce 的重大更改。

特别是,此迁移中断了计划使用 AlarmManager 启动服务的任何 Alarm 以及添加到 的任何 Actions启动服务的通知


解决方案

Replace PendingIntent.getService with PendingIntent.getBroadcast that starts a BroastcastReceiver.

This receiver then starts the JobIntentService using enqueueWork.


迁移多个服务时,这可能会重复且容易出错。

为了使这更容易并且与服务无关,我创建了一个通用的 StartJobIntentServiceReceiver,它接受一个作业 ID 和一个用于 JobIntentServiceIntent

当接收器启动时,它将启动带有作业 ID 的最初预期的 JobIntentService,并将 Intent 的原始内容实际转发到幕后服务.

/**
* A receiver that acts as a pass-through for enqueueing work to a {@link android.support.v4.app.JobIntentService}.
*/
public class StartJobIntentServiceReceiver extends BroadcastReceiver {

public static final String EXTRA_SERVICE_CLASS = "com.sg57.tesladashboard.extra_service_class";
public static final String EXTRA_JOB_ID = "com.sg57.tesladashboard.extra_job_id";

/**
* @param intent an Intent meant for a {@link android.support.v4.app.JobIntentService}
* @return a new Intent intended for use by this receiver based off the passed intent
*/
public static Intent getIntent(Context context, Intent intent, int job_id) {
ComponentName component = intent.getComponent();
if (component == null)
throw new RuntimeException("Missing intent component");

Intent new_intent = new Intent(intent)
.putExtra(EXTRA_SERVICE_CLASS, component.getClassName())
.putExtra(EXTRA_JOB_ID, job_id);

new_intent.setClass(context, StartJobIntentServiceReceiver.class);

return new_intent;
}

@Override
public void onReceive(Context context, Intent intent) {
try {
if (intent.getExtras() == null)
throw new Exception("No extras found");


// change intent's class to its intended service's class
String service_class_name = intent.getStringExtra(EXTRA_SERVICE_CLASS);

if (service_class_name == null)
throw new Exception("No service class found in extras");

Class service_class = Class.forName(service_class_name);

if (!JobIntentService.class.isAssignableFrom(service_class))
throw new Exception("Service class found is not a JobIntentService: " + service_class.getName());

intent.setClass(context, service_class);


// get job id
if (!intent.getExtras().containsKey(EXTRA_JOB_ID))
throw new Exception("No job ID found in extras");

int job_id = intent.getIntExtra(EXTRA_JOB_ID, 0);


// start the service
JobIntentService.enqueueWork(context, service_class, job_id, intent);


} catch (Exception e) {
System.err.println("Error starting service from receiver: " + e.getMessage());
}
}

}

您需要将包名称替换为您自己的包名称,并照常在您的 AndroidManifest.xml 中注册此 BroadcastReceiver:

<receiver android:name=".path.to.receiver.here.StartJobIntentServiceReceiver"/>

您现在可以在任何地方安全地使用 Context.sendBroadcastPendingIntent.getBroadcast ,只需将要传递给您的 Intent 包装起来>JobIntentService 在接收者的静态方法中,StartJobIntentServiceReceiver.getIntent


例子

您可以启动接收器,并通过扩展您的 JobIntentService,立即执行以下操作:

Context.sendBroadcast(StartJobIntentServiceReceiver.getIntent(context, intent, job_id));

任何您不立即启动服务的地方都必须使用 PendingIntent,例如在使用 AlarmManager 安排 Alarms 或添加 ActionNotification:

PendingIntent.getBroadcast(context.getApplicationContext(),
request_code,
StartJobIntentServiceReceiver.getIntent(context, intent, job_id),
PendingIntent.FLAG_UPDATE_CURRENT);

关于android - Android O 从 IntentService 迁移到 JobIntentService 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46675242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com