need to access the system permission

后端 未结 1 1814
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 08:02

I am trying to access the folder of my app from data/data but it need to change the permission to 0777. So, I had used some code that can change at

1条回答
  •  情歌与酒
    2020-12-12 08:51

    Here, I developed one class for to give granted permission on marshmallow device.

    Getpermission.java

    public class GetPermission extends Activity {
    
    private static final int REQUEST_CODE = 2;
    private static final int REQUEST_PERMISSIONS = 10;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (Build.VERSION.SDK_INT >=23)
        {
            getPermission();
        }
        else
        {
            startService();
        }
    
    }
    
    private void getPermission()
    {
        if (ContextCompat.checkSelfPermission(GetPermission.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                + ContextCompat.checkSelfPermission(GetPermission.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
    
            ActivityCompat.requestPermissions(GetPermission.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    },
                    REQUEST_PERMISSIONS);
    
        }
        else
        {
            startService();
        }
    }
    private void startService()
    {
    
        //In this intent add your starting first activity
    
        Intent in = new Intent(getApplicationContext(),HomeScreen.class);
        startActivity(in)
        finish();
    }
    
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSIONS) {
            if ((grantResults.length > 0) && (grantResults[0]+grantResults[1]+grantResults[2])
                    == PackageManager.PERMISSION_GRANTED) {
    
                getWindowOverLayPermission();
            } else {
                Toast.makeText(GetPermission.this, "All Permission is required to use xyz", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }
    
    @TargetApi(Build.VERSION_CODES.M)
    private void getWindowOverLayPermission()
    {
        if (!Settings.canDrawOverlays(GetPermission.this))
        {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_CODE);
        }
        else
        {
            startService();
        }
    }
    
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    
        if (requestCode == REQUEST_CODE)
        {
            Toast.makeText(GetPermission.this, "Windows Overlay Permission is required",Toast.LENGTH_LONG).show();
            getWindowOverLayPermission();
        }
        else
        {
            startService();
        }
    
    }
    }
    

    After that change your Manifest.xml

    
    
    
    
    
    
        
            
                
    
                
            
        
        
    
    

    Hope,It will help you...

    0 讨论(0)
提交回复
热议问题