我目前能够在使用SuperSU的root设备上的应用中获得root访问权限。当我的应用程序请求root访问权限时,会显示来自SuperSu的对话框,然后用户点击"授予"允许权限。
我希望能够绕过这一步,让我的应用程序获得root策略的root权限,而无需点击" Grant" SuperSu对话框上的按钮。
我该怎么做?
答案 0 :(得分:8)
我希望能够绕过这一步,让我的应用程序以编程方式获得root权限,而无需点击" Grant" SuperSu对话框上的按钮。
你不能。用户必须授予您的应用root访问权限。除非您构建自己的SU管理应用程序,否则您无法绕过此初始权限请求。
我正在为应用提供设备,然后用户使用该设备(我拥有该设备)。只是为了提供更多上下文,当我发布设备时,它已经拥有了应用程序,并且已经被SuperSu授予root访问权限。因此,我试图避免的情况是,应用程序可能会松开权限,然后在现场进行再次请求。用户不懂技术,他们不知道SuperSu对话框是什么或者用它做什么。
如果您拥有该设备,则可以更改"默认访问权限"到"格兰特"在SuperSu设置中。您也可以按应用更改此内容。
您在评论中提到您的应用已拥有超级用户权限。您具有root访问权限,因此您可以更改SuperSu的共享首选项以始终授予您的应用访问权限。同样,您暗示您拥有该设备,因此您不需要以编程方式修改SuperSu首选项。
如果您确实需要修改SuperSu的首选项,则以下代码应更改应用的访问设置,以便始终允许和禁用通知(使用android-shell):
@WorkerThread
public static boolean tryChangingSuperSuDefaultAccess(Context context) throws Exception {
String packageName = context.getPackageName();
PackageManager pm = context.getPackageManager();
// Get the preferences for SuperSu
Context packageContext = context.createPackageContext("eu.chainfire.supersu", 0);
SharedPreferences superSuPrefs = PreferenceManager.getDefaultSharedPreferences(packageContext);
File superSuPrefsFile = getSharedPreferencesFile(superSuPrefs);
// Copy SuperSu preferences to our app's shared_prefs directory
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
File directory = getSharedPreferencesFile(preferences).getParentFile();
File destination = new File(directory, "eu.chainfire.supersu.xml");
int uid = pm.getApplicationInfo(context.getPackageName(), 0).uid;
destination.getParentFile().mkdirs();
Shell.SU.run("cp \"" + superSuPrefsFile + "\" \"" + destination + "\"");
Shell.SU.run("chmod 0660 \"" + destination + "\"");
Shell.SU.run("chown " + uid + " " + uid + " \"" + destination + "\"");
// Now we can edit the shared preferences
superSuPrefs = context.getSharedPreferences("eu.chainfire.supersu", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = superSuPrefs.edit();
editor.putString(String.format("config_%s_notify", packageName), "no"); // disable SuperSu notifications
editor.putString(String.format("config_%s_access", packageName), "grant"); // Set access to grant for this app
// noinspection all
editor.commit();
// Copy the edited shared preferences back
return Shell.SU.run("cp \"" + destination + "\" \"" + superSuPrefsFile + "\"").isSuccessful();
}
private static File getSharedPreferencesFile(SharedPreferences preferences)
throws NoSuchFieldException, IllegalAccessException {
Field field = preferences.getClass().getDeclaredField("mFile");
if (!field.isAccessible()) field.setAccessible(true);
return (File) field.get(preferences);
}
我不建议使用上面的代码。您应该透明并教育您的用户。如果您拥有该设备(如声明的那样),则可以使用该应用自行更改这些设置。