问题描述
- GPS_PROVIDER无法获取定位
-
代码如下public class MainActivity extends AppCompatActivity { LocationManager locManager; EditText show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (EditText) findViewById(R.id.EditText); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);} locManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); Location location = locManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); updateView(location); locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 3000, 8, new LocationListener() // ① { @Override public void onLocationChanged(Location location) { updateView(location); Log.v("MSG","1"); } @Override public void onProviderDisabled(String provider) { updateView(null); Log.v("MSG","2"); } @Override public void onProviderEnabled(String provider) { if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);} updateView(locManager .getLastKnownLocation(provider)); Log.v("MSG","3"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }); } public void updateView(Location newLocation) { if (newLocation != null) { StringBuilder sb = new StringBuilder(); sb.append("实时的位置信息:n"); sb.append("经度:"); sb.append(newLocation.getLongitude()); sb.append("n纬度:"); sb.append(newLocation.getLatitude()); sb.append("n高度:"); sb.append(newLocation.getAltitude()); sb.append("n速度:"); sb.append(newLocation.getSpeed()); sb.append("n方向:"); sb.append(newLocation.getBearing()); show.setText(sb.toString()); } else { show.setText("无法获取地理信息"); Log.v("MSG","4"); } } }
权限都已申请,还根据6.0的新变化,增加了运行时权限申请,但是还是无法获得定位信息,
把GPS_PROVIDER换成NETWORK_PROVIDER就可以获得定位,咋回事?
时间: 2024-09-17 12:07:04