StartActivity once user click Allow on Permission

2019-07-13 03:25发布

问题:

My one activity need Location Permission and I wrote below code to get permission. But in this case user need to click twice to open activity if app does not have location permission initially. Can I make some change so that once user click allow on Permission screen, only then intent fires.

 int PERMISSION_ALL = 1;
                            String[] PERMISSIONS = new String[0];
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                PERMISSIONS = new String[]{
                                        Manifest.permission.ACCESS_FINE_LOCATION};
                            }

                            if (!hasPermissions(getApplicationContext(), PERMISSIONS)) {
                                ActivityCompat.requestPermissions(StartupActivity.this, PERMISSIONS, PERMISSION_ALL);
                            }

                            if (hasPermissions(getApplicationContext(), PERMISSIONS)) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent(StartActivity.this, Details.class);
                                        startActivity(intent);
                                    }
                                }).start();
                            }

回答1:

Yes, you need to override onRequestPermissionsResult

First on onCreate

int PERMISSION_ALL = 1;
String[] PERMISSIONS = new String[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION};
}

if (!hasPermissions(getApplicationContext(), PERMISSIONS)) {
    ActivityCompat.requestPermissions(StartupActivity.this, PERMISSIONS, PERMISSION_ALL);
}

then Override

  @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSION_ALL) {
                Intent intent = new Intent(StartActivity.this,Details.class);
                startActivity(intent);
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


回答2:

override onRequestPermissionsResult and if the permission is granted then start your Activity from there

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_ALL){
        if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){

             new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(StartActivity.this, Details.class);
                        startActivity(intent);
                    }
                }).start();
        }else{
            Toast.makeText(MainActivity.this, "Access Denied ! Cannot proceed further ", Toast.LENGTH_SHORT).show();
        }
    }
}

Note : Apparently , the code seems like in StartupActivity (from StartupActivity.this ) so you don't required to create a thread , simply start your Details Activity with simple Intent


Code will be

class StartupActivity extends ..{

    onCreate...(){}

    // here you will have onRequestPermissionsResult 
    @Override
    public void onRequestPermissionsResult( ....){...}

}


回答3:

When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to requestPermissions(). For example, if an app requests READ_CONTACTS access it might have the following callback method:

@Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // 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.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }


回答4:

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

// Handle your permission grant/reject scenarios here, 
for( int x = 0; x < grantResults.length; x++ )
{
    if( grantResults[x] == PackageManager.PERMISSION_GRANTED )
    {
         // Since you had one, start your new activity here
    }
    else
    {
        // Do something with the permissions that got BLOCKED??
    }
}

}

This will be internally called by android, once user grants/rejects a permission