0

私は Android-M ランタイム権限に取り組んでいました。フラグメントの 1 つに場所のアクセス許可を追加したいと考えていました。これが私のコードです。Androidは許可を求めますが、許可を拒否しても何も起こらず、ダイアログボックスを閉じますが、それでもactivateGPS()機能して値を返します。前もって感謝します。

if (Build.VERSION.SDK_INT >= 23) {
        // Marshmallow+
        getPermissionToReadUserContacts();
    } else {
        // Pre-Marshmallow
        System.out.println("CODMOB: Device not marshmallow");
        activateGPS();
    }


    activateGPS();

    return view;
}
private static final int LOCATION_PERMISSIONS_REQUEST = 1;
public void getPermissionToReadUserContacts() {
    System.out.println("CODMOB: getPermissionToReadUserContacts()");
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been granted)
    // since the user can revoke permissions at any time through Settings

        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // The permission is NOT already granted.
            // Check if the user has been asked about this permission already and denied
            // it. If so, we want to give more explanation about why the permission is needed.
            System.out.println("CODMOB: The permission is NOT already granted.");
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {
                // Show our own UI to explain to the user why we need to read the contacts
                // before actually requesting the permission and showing the default UI
                System.out.println("CODMOB: We need to access location.");
            }else{

            // Fire off an async request to actually get the permission
            // This will show the standard permission request dialog UI
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    LOCATION_PERMISSIONS_REQUEST);

            }
        }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == LOCATION_PERMISSIONS_REQUEST) {
        if (grantResults.length>0 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            System.out.println("CODMOB:permission granted");
            activateGPS();
            //Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
        } else {
            System.out.println("CODMOB: permission denied");
            //Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
4

3 に答える 3

1

コードを次のように変更します

if (Build.VERSION.SDK_INT >= 23) {
        // Marshmallow+
        getPermissionToReadUserContacts();
    } else {
        // Pre-Marshmallow
        System.out.println("CODMOB: Device not marshmallow");
        activateGPS();
    }
    return view;
}

フラグメントにもかかわらず....このメソッドをアクティビティにも追加します

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.containerView);
        if (fragment != null) {
            fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
于 2016-03-30T12:18:07.910 に答える
0
 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) !=
            PackageManager.PERMISSION_GRANTED) {
        final String[] permission = new String[]{
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.INTERNET,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.PACKAGE_USAGE_STATS,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
        };
        ActivityCompat.requestPermissions(this, permission, 0);
    }

上記のコードをアクティビティで使用すると役立つ場合があります。

于 2016-12-30T12:21:29.730 に答える
0
 ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},       100);

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

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

            // permission was granted, yay! Do the
            // contacts-related task you need to do.          
        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
      }

    // other 'case' lines to check for other
    // permissions this app might request
    }
}
于 2016-09-14T06:32:52.947 に答える