【问题标题】:need to access the system permission需要访问系统权限
【发布时间】:2017-03-23 03:20:30
【问题描述】:

我正在尝试从data/data 访问我的应用程序的文件夹,但它需要将权限更改为0777。因此,我使用了一些可以在运行时更改但权限没有更改的代码。它给了我错误open failed: EACCES (Permission denied)。我还将这个权限放在棉花糖下面的清单文件中,我需要在根资源管理器中提供,我们更改文件夹 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());
}

【问题讨论】:

标签: java android file-permissions


【解决方案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>

希望对你有所帮助...

【讨论】:

  • 对不起,但不像我们在根资源管理器 rwxrwrw 中提供的这些权限
  • 您在哪个 android 版本中测试此应用??
  • Android 版本 4.4.4 kitkat
  • 我看到没有root访问权限我无法更改root explorer之类的权限
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 2011-05-05
相关资源
最近更新 更多