我的设备:

Android 6.0.1,这是我的应用程序设置.

compileSdkVersion 24

buildToolsVersion "24.0.1"

minSdkVersion 21

targetSdkVersion 24

从不调用LocationListener onLocationChanged().它总是调用onProviderDisabled().

这是MainActivity.java

TextView mTextView;

LocationManager mLocationManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTextView = (TextView) findViewById(R.id.tv);

if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return;

}

mLocationManager = (LocationManager) this

.getSystemService(Context.LOCATION_SERVICE);

mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1 * 1000,0.00001F,locationListener);

}

private void updateUIToNewLocation(Location location) {

Log.d("hello","updateUIToNewLocation = ");

if (location != null) {

mTextView.setText("Latitude:" + location.getLatitude() + "\nLongitude:"

+ location.getLongitude());

} else {

mTextView.setText("error");

}

}

LocationListener locationListener = new LocationListener() {

public void onLocationChanged(Location location) {

Log.d("hello","onLocationChanged");

System.out.println("onLocationChanged");

System.out.println("Latitude:" + location.getLatitude() + "\nLongitude"

+ location.getLongitude());

updateUIToNewLocation(location);

}

public void onStatusChanged(String provider,int status,Bundle extras) {

Log.d("hello","onStatusChanged");

System.out.println("onStatusChanged");

System.out.println("privider:" + provider);

System.out.println("status:" + status);

System.out.println("extras:" + extras);

}

public void onProviderEnabled(String provider) {

Log.d("hello","onProviderEnabled");

System.out.println("onProviderEnabled");

System.out.println("privider:" + provider);

}

public void onProviderDisabled(String provider) {

Log.d("hello","onProviderDisabled");

System.out.println("onProviderDisabled");

System.out.println("privider:" + provider);

}

};

这是manifest xml中的权限和Activity标签.

android:minSdkVersion="23"

android:targetSdkVersion="24"/>

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

android:name=".MainActivity"

android:label="@string/app_name">

此应用程序可以在Android 5.1上运行,但在Android 6.0.1中不起作用.希望有人可以帮助我.我不明白这个问题.

Logo

一站式 AI 云服务平台

更多推荐