Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Location LocationListener always call onProviderDisabled

My device: Android 6.0.1 and here is my application setup.

compileSdkVersion 24
buildToolsVersion "24.0.1"

minSdkVersion 21
targetSdkVersion 24

LocationListener onLocationChanged() is never called. It always call onProviderDisabled().

Here's 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);
    }
};

And here's the permissions and Activity tags in manifest xml.

<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>

This application can be run right on the Android 5.1,but in Android 6.0.1 is not functional. Hope someone could help me. I don't understand the problem.

like image 561
kai hello Avatar asked Oct 19 '22 01:10

kai hello


1 Answers

I was also facing same issue, in my case solution, I find out was that In Mobile go to Setting->Location->Location mode-> change to appropriate mode(out of provider you have used). Hope works in your case as well.

like image 124
beast98 Avatar answered Oct 21 '22 02:10

beast98