位置在 android 中不断更新,而不是一次
Posted
技术标签:
【中文标题】位置在 android 中不断更新,而不是一次【英文标题】:Location updates constantly in android instead of once 【发布时间】:2021-01-30 17:04:05 【问题描述】:我正在关注如何获取用户位置的教程,问题是我应该在发送位置时只获取一次位置坐标,而不是无限次,但我一直在获取位置坐标。
只有当他发送位置而不是经常喜欢我时,教程人才会获取位置。 为什么会发生这种情况,我该如何解决?
当用户更改位置时,我只想在日志中获取一次位置数据,而不是无限次。请帮忙。
我正在使用:- 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]]
【问题讨论】:
【参考方案1】:没有办法做到这一点。您所能做的就是在其上方编写包装器并在值更改时发送位置坐标。
【讨论】:
这不是问题,这是标准行为以上是关于位置在 android 中不断更新,而不是一次的主要内容,如果未能解决你的问题,请参考以下文章