需要访问系统权限

时间:2016-11-09 10:33:53

标签: java android file-permissions

我正在尝试从data/data访问我的应用的文件夹,但需要将权限更改为0777。所以,我使用了一些可以在运行时更改的代码,但权限没有改变。它给了我错误open failed: EACCES (Permission denied)。我还把这个权限放在Marshmallow下面的清单文件中,我需要在root explorer中给出我们更改文件夹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());
}

1 个答案:

答案 0 :(得分:1)

在这里,我开发了一个类来授予棉花糖设备许可。

<强> 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>

希望,它会帮助你...