You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中LocationRequest.create()已弃用,如何用Builder创建位置请求?

替换弃用的LocationRequest.create()方法(Java实现)

原来的LocationRequest.create()已被弃用,改用LocationRequest.Builder构建实例的方式有两种写法,都能得到你需要的locationRequest变量:

链式调用(简洁写法)

LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
        .setFastestInterval(5000)
        .build();

分步构建(清晰写法)

// 初始化Builder,传入优先级和基础间隔时间
LocationRequest.Builder requestBuilder = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000);
// 设置最快更新间隔
requestBuilder.setFastestInterval(5000);
// 构建得到LocationRequest实例
LocationRequest locationRequest = requestBuilder.build();

参数对应说明

  • 原代码的setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) → 对应Builder构造器第一个参数Priority.PRIORITY_HIGH_ACCURACY(若使用旧版本依赖,仍可传入LocationRequest.PRIORITY_HIGH_ACCURACY)
  • 原代码的setInterval(10000) → 对应Builder构造器第二个参数(单位:毫秒)
  • 原代码的setFastestInterval(5000) → 保留setFastestInterval()方法调用

构建完成后,locationRequest变量就可以像之前一样用于程序化修改位置设置。

内容的提问来源于stack exchange,提问作者Mukesh Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:01:14