- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用带有 JobScheduler 和 JobService 的新 FusedLocationProviderClient 创建后台位置跟踪服务。我读过 here
我们应该使用 requestLocationUpdates (LocationRequest request, PendingIntent callbackIntent)
以此目的。我在我的 MainActivity 中安排工作是这样的:
final ComponentName serviceComponent = new ComponentName(this, LocationJobService.class);
final JobScheduler jobScheduler;
final JobInfo.Builder builder = new JobInfo.Builder(LocationJobService.LOCATION_JOB_SERVICE_ID,
serviceComponent);
builder.setPeriodic(JobInfo.getMinPeriodMillis(), JobInfo.getMinFlexMillis());
builder.setPersisted(true);
final JobInfo jobInfo = builder.build();
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
if (jobScheduler != null)
{
if(jobScheduler.schedule(jobInfo) == JobScheduler.RESULT_SUCCESS)
Log.d(TAG, String.format("Job %s successfully scheduled", LocationJobService.LOCATION_JOB_SERVICE_ID));
else
Log.e(TAG, "Job schedule failed!");
}
LocationJobService 类:
private static final String TAG = "Class " + LocationJobService.class.getSimpleName();
public static final int LOCATION_JOB_SERVICE_ID = 1001;
private FusedLocationProviderClient client;
@Override
public boolean onStartJob(final JobParameters params)
{
//some logic before initializing location update
initializeLocationUpdate(getApplicationContext());
}
private void initializeLocationUpdate(Context context)
{
client = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(1000);
client.requestLocationUpdates(locationRequest, pendingIntent)
.addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
Log.d(TAG, "onComplete " + task.getResult());
}
}).addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Log.d(TAG, "onFailure");
}
});
}
关于如何为 requestLocationUpdates
提供 PendingIntent 的任何想法以及如何获得 Location
来自 onComplete(@NonNull Task<Void> task)
?
最佳答案
我想出的解决方案是我创建了扩展 BroadcastReceiver
的 Receiver,然后用它来创建 PendingIntent
。然后从我在 onReceive
方法中获得的 Intent 中提取位置。在 JobService
中:
client = LocationServices.getFusedLocationProviderClient(getApplicationContext());
Intent locationReceiverIntent = new Intent(getApplicationContext(), LocationJobServiceReceiver.class);
locationReceiverIntent.setAction(LocationJobServiceReceiver.PROCESS_UPDATES);
PendingIntent locationReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), LocationJobServiceReceiver.LOCATION_JOB_SERVICE_RECEIVER_CODE, locationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(30000);
locationRequest.setExpirationDuration(60000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (checkPermission(getApplicationContext()))
{
client.requestLocationUpdates(locationRequest,locationReceiverPendingIntent);
jobFinished(jobParameters, false);
}
在 LocationJobServiceReceiver
的 onReceive(final Context context, Intent intent)
中:
client = LocationServices.getFusedLocationProviderClient(context);
final LocationResult locationResult = LocationResult.extractResult(intent);
final String action = intent.getAction();
pendingIntent = PendingIntent.getBroadcast(context, LOCATION_JOB_SERVICE_RECEIVER_CODE, intent, PendingIntent.FLAG_NO_CREATE);
if (PROCESS_UPDATES.equals(action))
{
if (locationResult != null)
{
Location location;
List<Location> locations = locationResult.getLocations();
if (locations.size() > 0)
{
location = locations.get(0);
}
else
{
location = locationResult.getLastLocation();
}
}
}
client.removeLocationUpdates(pendingIntent);
关于android - 使用 FusedLocationProviderClient、JobScheduler 和 JobService 更新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170499/
For my specific use case, I need to fetch the device's real-time location updates and pass them t
我已经成功创建了一个带有 的 Activity FusedLocationProviderClient 返回特定时间段内的位置坐标。 但我希望它在后台服务中运行,该服务将坐标保存在本地数据库中,并且在
我在 android 中使用 FusedLocationProviderClient 类来获取用户的最后位置。一切都很好,我可以获得纬度和经度,但是 task.getResult().getBeari
在我的一项 Activity 中,我使用 FusedLocationProviderClient 来获取持续的位置更新。我的代码基于此方法:https://developer.android.com/
我尝试在 Android 上使用新的简化位置 API(在 GPS 11.0 中引入)。我编写了一个实现位置 API 的服务。但它只会触发并发送位置几秒钟(只有 2 次)。 public cla
public void getDeviceLocation() { fusedLocationClient = LocationServices.getFusedLocationPro
我刚刚将我的 Google 依赖项更新到 12.0.1,现在我在使用 FusedLocationProviderClient 时遇到此错误。 知道为什么会这样吗?查看源代码,我可以看到构造函数现在被隐
我在声明时遇到错误无法解析符号 'FusedLocationProviderClient' private FusedLocationProviderClient mFusedLocationClie
LocationRequest 现已弃用?它的替代品是什么? 我正在使用它,但最近收到了一条弃用消息。我现在应该做什么: val locationRequest = LocationRequest()
当接近传感器被激活时,我使用 FusedLocationProviderClient 来获取设备位置。当用户靠近接近传感器时,程序应该获取设备位置,但它没有获取必要的代码,因为我认为它卡在了 .isS
我在使用 Fused location Provider Api 的 Android 应用程序中遇到了一些奇怪的行为。 在某些设备中,此 api 不返回高度 我可以在 Moto G 2d genera
我正在实现 Xamarin Android 位置跟踪器并添加以下代码以停止位置更新。 public Task StopAsync() { if(IsTracking)
很抱歉,如果这真的很简单(也未能在 Marshmallow 模拟器上安装 google play 服务,并且有点紧张!)。 我的目标是在 Visual Studio 2017 (15.6.3) 上的
我通过单击按钮在我的应用程序中获取与 FusedLocationProviderClient 的坐标以存储到数据库中。问题是,当我重新启动手机或模拟器时,.getLastLocation() 为 nu
随着 FusedLocationProviderApi 被弃用,我很难使用 FusedLocationProviderClient 在单击按钮后仅一次获取用户的位置。 最佳答案 I'm having
我不明白为什么这个非常简单的 Activity 会产生内存泄漏。 它遵循此处给出的指南:https://developer.android.com/training/location/receive-
我曾经使用 FusedLocationApi,直到我得知它已被弃用(请参阅下面的引用资料)。实现起来很简单。正如文档所说,您需要将它与 GoogleApiClient 结合使用 LocationSer
我想使用最新的融合位置提供程序客户端在“后台服务”上获取位置更新。我不想使用所有人都在使用的位置监听器和 Google API 客户端。我还需要使用 google play 服务提供的位置设置 Api
我有一个 activity 扩展了一个名为 LocationAwareActivity 的基 class 所有这些 LocationAwareActivity activity 做的是创建一个定位服务
自从新的 Android 8 更新发布以来,有很多 limitations 强加于后台服务的工作方式。 所以我的应用需要在后台定期获取用户的位置,为此,我考虑使用 Android 网站建议的 Fuse
我是一名优秀的程序员,十分优秀!