2016-11-09 165 views
-2

我試圖從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()); 
} 
+0

您需要添加運行時間在訪問任何危險權限之前。 –

+0

'ls'不適合更改權限。 –

+0

https://developer.android.com/training/permissions/requesting.html –

回答

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> 

希望,它會幫助你...

+0

抱歉,但沒有這些權限像我們給根探險rwxrwrw –

+0

在Android版本要測試這個程序寫的權限? –

+0

Android版本4.4.4奇巧 –