Android中的分组权限

时间:2018-10-30 02:40:51

标签: android android-permissions

在我解释我的情况之前,这里是我的代码。

public class Permission extends AppCompatActivity  implements ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<LocationSettingsResult> {
    public static final int MULTIPLE_PERMISSIONS = 10;
    Timer myTimer;
    TimerTask doThis;

    protected GoogleApiClient mGoogleApiClient;
    protected LocationRequest locationRequest;
    int REQUEST_CHECK_SETTINGS = 100;

    String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        mGoogleApiClient.connect();
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);

        /* TODO : Grant the following permissions
         * 1. Request Read External Storage OK
         * 2. Request Write External Storage OK
         * 3. Request Read Intenal Storage OK
         * 4. Request Write External Storage OK
         * 5. Request GPS (2) OK
         * 6. Request Camera OK
         * */

        if (checkPermissions()) {
            Intent standby = new Intent(Permission.this, standby.class);
            startActivity(standby);
        }

        Button btn_enable = findViewById(R.id.enable);
        btn_enable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EnablePermissions();
            }
        });

        /* Check if permission is done */
        myTimer = new Timer();
        int delay = 0;
        int period = 1000;
        doThis = new TimerTask() {
            public void run() {
                if (checkPermissions()) {
                    myTimer.cancel();
                    Intent standby = new Intent(Permission.this, standby.class);
                    startActivity(standby);
                }
            }
        };

        myTimer.scheduleAtFixedRate(doThis, delay, period);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, ""+requestCode, Toast.LENGTH_SHORT).show();
        if (requestCode == REQUEST_CHECK_SETTINGS) {
            if (resultCode == RESULT_OK) {
                //Toast.makeText(getApplicationContext(), "GPS enabled", Toast.LENGTH_LONG).show();
            } else {
                //Toast.makeText(getApplicationContext(), "GPS is not enabled", Toast.LENGTH_LONG).show();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    private boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
           return false;
        }
        return true;
    }

    private boolean EnablePermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(
                        mGoogleApiClient,
                        builder.build()
                );
        result.setResultCallback(this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                // NO need to show the dialog;
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                //  GPS turned off, Show the user a dialog
                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(Permission.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    //failed to show dialog
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                // Location settings are unavailable so not possible to show any dialog now
                break;
        }
    }
}

其输出就是这个。

enter image description here

enter image description here

我的问题是我怎样才能做到这一点?我的意思是允许每个人允许的组权限。

我不知道该怎么办。我所指的是将所有权限合并为一个,然后,如果用户未选择最后一个,则仍然可以。

很抱歉造成混乱。根据我的代码,第一个权限启用3个设置,第二个权限启用位置服务。基本上,它们两个都位于不同的对话框中,使2出现两次。我想要将它们合并为一个组权限(基于第一个图像),因此它将使用一个对话框和一个对话框,该对话框允许用户启用以下功能(存储,相机,位置和位置服务)

1 个答案:

答案 0 :(得分:0)

首先,您的代码似乎缺少onRequestPermissionsResult方法,该方法可以处理用户接受/拒绝个人权限后发生的情况。如该链接所示,您可以使用switch语句并对每个权限使用逻辑,因此您可以决定哪个是可选的,哪个是强制的,并相应地更改UI和应用程序的行为。

第二,如果您要问的是是否可以在单个对话框中授予所有权限(来自多个组)(而不是每个组一个),那么这是不可能的。

根据official docs

  

注意:当您的应用程序调用requestPermissions()时,系统会向用户显示一个标准对话框。您的应用无法配置或更改该对话框。如果您需要向用户提供任何信息或说明,则应在调用requestPermissions()之前执行此操作,如解释应用程序为何需要权限中所述。

因此,在您的情况下,用户将获得3个权限对话框-每个分别针对“存储”,“联系人”和“位置”权限组。

最后一个提示用户启用位置的对话框与权限无关,只是切换了GPS设置。