同时适用于 android 6.0 与 5.0 的权限问题的解决办法(以地理位置信息为例)

访问量: 486

refer to :  http://stackoverflow.com/questions/33865445/gps-location-provider-requires-access-fine-location-permission-for-android-6-0

今天我们的手机app 遇到的问题是 在小米5上 一用就崩溃.  

后来使用腾讯的云测试,发现是我们的地理位置信息不对造成的. error log: 

10-28 15:36:22.799: D/AndroidRuntime(30373): Shutting down VM
10-28 15:36:22.799: D/AndroidRuntime(30373): --------- beginning of crash
10-28 15:36:22.800: E/AndroidRuntime(30373): FATAL EXCEPTION: main
10-28 15:36:22.800: E/AndroidRuntime(30373): Process: com.tuling, PID: 30373
10-28 15:36:22.800: E/AndroidRuntime(30373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tuling/com.tuling.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
10-28 15:36:22.800: E/AndroidRuntime(30373): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2432)

解决办法: (感觉java代码好啰嗦) 

        Log.d(TAG, "======before get location..");

        // for android 6.0
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "== in android 6.0, getting permission");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else{
            Log.d(TAG, "== in android 5.0");
            get_location();
        }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    get_location();
                } else {
                    Log.d(TAG, "=== NOT got granted");
                }
                return;
            }
        }
    }

    private void get_location() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TulingLocationListener());

//        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, false);
//        Location location = locationManager.getLastKnownLocation(bestProvider);

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        Log.d(TAG, "=====location===" + location);

        if (null != location) {
            Log.d(TAG, "开始设置经纬度");

            lat1 = location.getLatitude();
            long1 = location.getLongitude();
        } else {
            // beijing office location
            lat1 = 39.993669;
            long1 = 116.470126;
        }

        Log.d(TAG, "======after get location..=== lat:" + lat1 + "===lon:" + long1);

        showFragment(TRAVEL_ASSISTANT_CODE);
    }

订阅/RSS Feed

Subscribe

分类/category