android - 位置在 android 中不断更新,而不是一次
问题描述
我正在关注如何获取用户位置的教程,问题是我应该在发送位置时只获取一次位置坐标,而不是无限次,但我一直在获取位置坐标。
教程人员仅在发送位置时才获得位置,而不是一直像我一样。为什么会发生这种情况,我该如何解决?
当用户更改位置而不是无限次时,我只想在日志中获取一次位置数据。请帮忙。
我正在使用:- Android Studio 版本:- 4.0.1
AVD:- 像素 2 API 26
这是代码: -
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("Location",location.toString());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
这是来自 android studio 的 logcat:-
2020-10-16 12:39:46.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:46.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m56s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s930ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:47.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m57s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.245 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s931ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:48.248 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m58s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.238 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:49.241 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h27m59s930ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.231 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s926ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:50.235 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m0s927ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.236 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:51.243 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m1s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s929ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.240 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.331100,-122.390442 hAcc=20 et=+1d8h28m2s928ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.273 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:52.277 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m2s967ms vel=0.0 bear=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
2020-10-16 12:39:53.237 15537-15537/com.example.locationdemo2fortesting I/Location: Location[gps 40.356222,-122.415847 hAcc=20 et=+1d8h28m3s929ms alt=0.0 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]
解决方案
没有办法做到这一点。您所能做的就是在其上方编写包装器并在值更改时发送位置坐标。
推荐阅读
- html - 在伪类之前背景不工作
- firebase - 如何使用 Vuejs 显示 firebase 返回的确切错误
- javascript - 如何在 Vuetify 中扩展 v-data-table 组件中的插槽?
- ruby-on-rails - 如何从 Webhook Ruby on Rails 获取不记名令牌
- git - 检查 git 存储库中的总体进度
- javascript - 如何使用 Jest 和 supertest 将多个测试服放在一个测试服中?
- machine-learning - 荷兰语文本的数据清洗 - 情感分析
- javascript - 向 Firestore 文档添加密钥的问题
- keras - 为什么 EfficientNet B0 的输出是二维的?
- dataframe - 从具有目标列值的列更新 pyspark 数据框