当前位置: 首页 > 知识库问答 >
问题:

如何(何时?)如果可能的话,取消LocationManager.RequestSingleUpdate?

公西国发
2023-03-14
public void getGPS(View view) {
    // load all available Location providers
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(false);
    // determine the last known location within 2 hours available from cache
    Location myLocation = null;
    Date now = new Date();
    long time = now.getTime();
    long timeDiff = LOCATION_MAX_AGE;
    for (int i=providers.size()-1; i>=0; i--) {
        Location l = locationManager.getLastKnownLocation(providers.get(i));
        if (l != null) {
            long t = l.getTime();
            if (time - t < timeDiff) {
                myLocation = l;
                time = t;
                timeDiff = time - t;
            }
        }
    }
    // if failed to get cached location or if it is older than 2 hours, request GPS position
    if (myLocation == null) {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            pIntent = PendingIntent.getBroadcast(context,
                    0,
                    new Intent(SINGLE_UPDATE_ACTION),
                    PendingIntent.FLAG_UPDATE_CURRENT);

            IntentFilter iFilter = new IntentFilter(SINGLE_UPDATE_ACTION);
            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // when received an answer from the LocationManager
                    Location gpsLocation = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
                    fillLocation(gpsLocation);
                    Log.d(TAG, "Received GPS location: " + gpsLocation.getLatitude() + ", " + gpsLocation.getLongitude() + " Time: " + gpsLocation.getTime());
                    locationManager.removeUpdates(pIntent);
                    unregisterReceiver(receiver);
                }
            };
            context.registerReceiver(receiver, iFilter);
            // I'm absolutely fine with getting the first available GPS position and that's enough to me.
            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, pIntent);
         // if GPS is disabled in settings allow user to enable it
        } else {
            showGPSDisabledAlertToUser();
        }
    } else {
        // if fresh enough lastKnownLocation is found
        fillLocation(myLocation);
    }
}

共有1个答案

巫马安怡
2023-03-14

我建议您使用ProgressDialog来通知正在等待修复的用户。您还可以设置消息并向其添加cancel按钮。查看此链接:如何设置取消按钮在进行对话框?

如果用户取消ProgressDialod,那么您将以手动输入请求启动屏幕。

问候。

 类似资料:
  • 问题内容: 我有 如果正在使用entitymanager,如何检索会话?如何从分离标准中获取结果? 问题答案: 为了完全详尽无遗,如果您使用的是JPA 1.0或JPA 2.0实现,则情况有所不同。 JPA 1.0 对于JPA 1.0,您必须使用。但是请记住, 此方法的结果是特定 于 实现的, 即从使用Hibernate的应用程序服务器到其他服务器之间不可移植。例如,使用JBoss,您可以执行以下操

  • 本文向大家介绍proimise 如何取消相关面试题,主要包含被问及proimise 如何取消时的应答技巧和注意事项,需要的朋友参考一下 No description provided.

  • 本文向大家介绍如何取消promise?相关面试题,主要包含被问及如何取消promise?时的应答技巧和注意事项,需要的朋友参考一下 Promise/A+标准规定了:原Promise对象跟新返回的对象状态一致。所以可以通过返回一个始终是pending状态的Promise对象来取消Promise。 Promise.resolve().then(() => { console.log(1) return

  • 本文向大家介绍Android中如何取消listview的点击效果,包括了Android中如何取消listview的点击效果的使用技巧和注意事项,需要的朋友参考一下 在xml文件里面有listselecter的属性引用 引用transparent之后会让点击效果透明化,昨天整了半天才搞出来,记录一下。 PS:去除或替换listview 默认的点击选中时的颜色 1.去除默认的点击选中时的颜色 (1)设

  • 我必须取消此系统(附件如下)。我正在获取这个值,但是我不能在服务中,而不是在活动中,以编程方式取消它。 我的问题是: null

  • 问题内容: 我正在使用Java 8可完成的期货。我有以下代码: 我使用runAsync计划执行等待闩锁的代码。接下来,我取消了将来,希望将被中断的异常抛出内部。但是似乎线程在await调用上仍然处于阻塞状态,即使将来被取消(断言通过)也永远不会抛出InterruptedException。使用ExecutorService的等效代码可以正常工作。是CompletableFuture中的错误还是我的