Hoa.Tran
Hoa.Tran

Reputation: 935

How I can set event when click allow in dialog permission android target >23

In my Splash Activity I run a handler delay when start app, now with device 6.0 ( target 23) I added dialog ask permission. So I want after click allow in dialog I will start my handler because if i start handler at the same time with check permission it will next activity although I can't click allow in dialog . How I can do it? here is my code:

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

    }

    private static String[] STR_ALL_PERMITTIONS = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.SEND_SMS,
    };
    private static final int RCODE_PERMISSION_ALL = 1;
    private int iAskingCounter = 0;

    private boolean doesAppNeedPermissions() {
        return android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1;
    }

    private void askPermissions() {
        if (doesAppNeedPermissions()) {
            Toast.makeText(this, "hi", Toast.LENGTH_SHORT).show();
            iAskingCounter++;
            try {
                boolean isPermitted = isPermittedAllPermissions(this, STR_ALL_PERMITTIONS);
                if (!isPermitted) {
                    if (iAskingCounter >= 20) {
                        startNextIntent();
                    } else {
                        ActivityCompat.requestPermissions(this, STR_ALL_PERMITTIONS, RCODE_PERMISSION_ALL);
                    }
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "helllo", Toast.LENGTH_SHORT).show();
        }

    }

    private void startNextIntent() {
        Intent in = new Intent(Main22Activity.this, Main2Activity.class);
        startActivity(in);
    }

    private boolean isPermittedAllPermissions(Context context, String... sArrPermissions) {
        if (context == null || sArrPermissions == null) return true;

        for (String permission : sArrPermissions) {
            boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
            if (shouldShow) {
                //ActivityCompat.finishAffinity(this);
            }
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }

        return true;
    }

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

        askPermissions();
    }
    public void playHandler(){ // where is I call it?
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent in = new Intent(Main22Activity.this, Main2Activity.class);
                startActivity(in);
            }
        }, 1000);
    }

Upvotes: 0

Views: 1186

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

Try this: in onRequestPermissionsResult() get the Allow event like:

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

        case RCODE_PERMISSION_ALL: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                // call your method
                Start_Handler()
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

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

Upvotes: 3

Related Questions