Java 在Android Studio中从未调用onLocationUpdate
我正在尝试获取用户的位置,但似乎从未触发onLocationUpdate,我尝试在多台设备上运行此应用程序,结果相同 在这里发布之前,我试着用谷歌搜索,找不到任何解决方案。并且,在清单文件中添加了Android Internet、GPS粗略和GPS精细权限 这是我的全部活动代码Java 在Android Studio中从未调用onLocationUpdate,java,android,android-studio,android-gps,Java,Android,Android Studio,Android Gps,我正在尝试获取用户的位置,但似乎从未触发onLocationUpdate,我尝试在多台设备上运行此应用程序,结果相同 在这里发布之前,我试着用谷歌搜索,找不到任何解决方案。并且,在清单文件中添加了Android Internet、GPS粗略和GPS精细权限 这是我的全部活动代码 public class AnotherActivity extends AppCompatActivity { LocationManager locationManager; LocationList
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);
}
}
}
在请求位置时,您是否也可以尝试添加网络提供商 RequestLocationUpdate(LocationManager.NETWORK\u提供程序,0,0,locationListener)