安卓定位LocationListener总是调用onProviderDisabled方法

5

我的设备: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);
    }
};

以下是清单文件中的权限和 Activity 标签。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.network"/>
<uses-feature android:name="android.hardware.location.gps" />

<uses-sdk
    android:minSdkVersion="23"
    android:targetSdkVersion="24"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

这个应用程序可以直接在Android 5.1上运行,但在Android 6.0.1上无法正常使用。希望有人能帮我解决问题,我不明白具体原因。

1
我知道这个。https://dev59.com/xVfUa4cB1Zd3GeqPK70a 我简直不敢相信这个问题困扰了我两天。 - kai hello
你找到解决方案了吗? - hasnain_ahmad
我提供的链接解决了这个问题。 - kai hello
1个回答

3

我也遇到了同样的问题,在我的情况下,解决方法是在手机上进行以下操作: 设置>位置>位置模式>更改为适当的模式(与您使用的提供程序不同)。 希望这个方法对您也有用。


1
谢谢,将位置精度设置为高是解决方案! - Sadik anass

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接