gpt4 book ai didi

java - Android Studio 中永远不会调用 onLocationUpdate

转载 作者:行者123 更新时间:2023-12-01 23:01:55 26 4
gpt4 key购买 nike

我正在尝试获取用户的位置,但似乎从未触发 onLocationUpdate,我尝试在多个设备上运行此应用程序,结果相同。

在发帖之前我尝试 Google 一下,但找不到任何解决方案。并且,在 list 文件中添加了 Android Internet、GPS COARSE 和 GPS FINE 权限。

这是我的整个 Activity 代码

public class AnotherActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

// Checking if we got permission
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_nearby_restaurants);

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(NearbyRestaurants.this, location.getLatitude()+String.valueOf(location.getLongitude()), Toast.LENGTH_SHORT).show();
Log.i("Location Update!", 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{
Log.i("Checking Location","Checking Location Wait");

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

}
}
}

最佳答案

您是否也可以尝试在请求位置时添加 NETWORK_PROVIDER

requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

关于java - Android Studio 中永远不会调用 onLocationUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58417222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com