Thursday, December 17, 2015

Grant Permission Android 6.0

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION_READ_CONTACT);    }
}

@Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_PERMISSION_READ_CONTACT:
                for (int i = 0; i < grantResults.length; i++) {
                    Log.i("GRANT_RESULT", i + ":" + grantResults[i]);                }
                Log.i("GRANT_RESULT", "Contact:" + String.valueOf(grantResults[0] == PackageManager.PERMISSION_GRANTED));                Log.i("GRANT_RESULT", "Local:" + String.valueOf(grantResults[1] == PackageManager.PERMISSION_GRANTED));
                // OTHER WAY//                if(checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){//                    Toast.makeText(this, "Deny", Toast.LENGTH_SHORT).show();//                }//                else {//                    Toast.makeText(this, "Allow", Toast.LENGTH_SHORT).show();//                }                break;            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);        }
    }




  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });