需要访问系统权限 [英] need to access the system permission

查看:87
本文介绍了需要访问系统权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从data/data访问我的应用程序的文件夹,但是它需要将权限更改为0777.因此,我使用了一些可以在运行时更改的代码,但是权限没有更改.它给我错误open failed: EACCES (Permission denied).我还将此许可权放在棉花糖下面的清单文件中,我需要像在root资源管理器中那样,在其中我们更改文件夹rwxrwxrw

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 run time but the permissions are not changing. It gives me error open failed: EACCES (Permission denied). I also put this permission in manifest file below Marshmallow i need to give like in root explorer where we change the folder rwxrwxrw

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是我的代码

String[] command = new String[]{"/system/bin/ls", "0777",
                        "/data/data/com.ayub.android.baba" };
    process = Runtime.getRuntime().exec(command);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
    int read;

    String output = "";

    String line;
    while ((line = reader.readLine()) != null) {
        output.concat(line + "\n");
        Log.w("myApp", "[[output]]:" + line);
        process.waitFor();
    }
    reader.close();
    process.waitFor();
} catch (Exception e) {
    Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
    Log.d(TAG,e.toString());
}

推荐答案

在这里,我开发了一个类来授予对棉花糖设备的许可权限.

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();
    }

}
}

此后,更改您的 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.christmashdwallpaper" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon_256"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme" >
    <activity android:name=".Permission.GetPermission" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Activity.HomeScreen"/>
</application>

希望,它将帮助您...

Hope,It will help you...

这篇关于需要访问系统权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆