android - 启动时的位置更新 - 在 Activity 中打开和关闭

标签 android location

我从谷歌开始 LocationUpdatesPendingIntent例子。 我已将位置信息从 Main Activity 移至 onBoot 广播接收器,因为我需要在设备启动时立即开始位置更新。这非常有效,并在状态栏中提供了通知。

但是我该如何打开和关闭 Activity 的位置更新呢?

这用于轮询车辆位置。

这是我的广播接收器:

public class StartupComplete1 extends BroadcastReceiver {

private static final long UPDATE_INTERVAL = 10000; // Every 10 seconds.
private static final long FASTEST_UPDATE_INTERVAL = 5000; // Every 5 seconds
private static final long MAX_WAIT_TIME = UPDATE_INTERVAL * 2; // Every 20 seconds.
private LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationClient;

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
        createLocationRequest();

        try {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, getPendingIntent(context));
        } catch (SecurityException e) {
            Toast.makeText(context, "Error - Cant start location updates", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

private PendingIntent getPendingIntent(Context context) {
    Intent intent = new Intent(context, LocationUpdatesBroadcastReceiver.class);
    intent.setAction(LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setMaxWaitTime(MAX_WAIT_TIME);
}

最佳答案

在广播接收器中开始检查位置更新并不是一个好主意。基于安卓documentation用于广播接收器

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app. Since these usually execute on the app's main thread, they are already bound by the ~5 second time limit of various operations that can happen there (not to mention just avoiding UI jank), so the receive limit is generally not of concern. However, once you use goAsync, though able to be off the main thread, the broadcast execution limit still applies, and that includes the time spent between calling this method and ultimately PendingResult.finish().

当位置更新需要更长的响应时间时,这可能会导致 ANR,尤其是当您在室内时。

您应该在启动完成的广播接收器的 onReceive() 上启动一个粘性服务。然后 MainActivity 可以绑定(bind)到该服务以执行必要的操作。

如果您的目标是 Android O,此方法可能会出现问题。请检查 this post这解释了 Android O 上的后台位置收集。

关于android - 启动时的位置更新 - 在 Activity 中打开和关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49417910/

相关文章:

java - Android中的RSA,解密未知 key 类型错误

android - 无法将视频共享到Youtube,这些视频位于/mnt/sdcard/Android/sd卡中的数据中

android - WhatsApp拦截个人资料照片上传

Android 最准确的 Location API

java - 在 java 中使用 IP 地址查找位置的示例代码?

android - 在 Android 中同时使用 GPS 和 Network Provider

java - Android 保存并检查,单选按钮

android - Jetpack Compose 1.0.0-alpha11如何手动导入扩展函数?

ios - 使用 Parse nearGeoPoint 检索用户位置

regex - 除特定 URI 外的所有位置的 Apache 配置