简体   繁体   中英

Android: Can't get current location after publish on play store

I used this code to get the current location and it works fine. But after publish in the store it just don't work anymore. The application stay locked in doInBackground function.

private class GpsAsync extends AsyncTask<Void, Integer, Boolean> {
    private ContactLocationListener contactLocationListener;
    private Location location;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        contactLocationListener = new ContactLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, contactLocationListener);
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        while (location == null) {}
        return null;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        textViewLatitude.setText(String.valueOf(location.getLatitude()));
        textViewLongitude.setText(String.valueOf(location.getLongitude()));
    }

    public class ContactLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            location = loc;
        }
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {}
        @Override
        public void onProviderEnabled(String s) {}
        @Override
        public void onProviderDisabled(String s) {}
    }
}

My manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

My check for permission:

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
} else {
    // start GpsAsync
}

Two things:

  1. The while loop in doInBackground leads to infinite loop every time when LocationProvider fails to provide location. It generally dangerous. Better to make some "guards" around - at least simple counter:

     protected Boolean doInBackground(Void... voids) { Integer count = 0 while (location == null||count<10000) {count++} return null; } 
  2. Regarding the question why provider returns null for location after publish. There several possible reasons, please check:

    • Is GPS available and turned-on on device?(I guess it may be tested on different device after publish, where GPS is turned-off)

    • Possible permission issues. Since API 23, for location and other "dangerous" services, in additional to declaring in manifest, it should be accepted once by user (pop-up when invoke the app for the first time). If user "deny" the service - GPS provider will always return null.

    • Also, consider to use "getBestProvider" in case if GPS is not available, like below

       LocationManager locationManager; String svcName = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(svcName); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true); String provider = locationManager.getBestProvider(criteria, true); locationManager.getLastKnownLocation(provider); l = locationManager.getLastKnownLocation(provider); 

Code to ask permission from user:

        // first check the permission in manifest:
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


            //  then ask for GPS permission from user:
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }

     ///////////////////////////////////////////////////////////////////////////////
     ///// NOTE: Even if we declared the permission in manifest, user can decline.
     /// So we anyway will do try/catch on pemission exception later on!
     ///////////////////////////////////////////////////////////////////////////////

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM