android - 如何在没有 SuperSU 的情况下授予 root 访问权限

标签 android

我目前可以使用 SuperSU 在已获得 root 权限的设备上获得我的应用程序的 root 访问权限。当我的应用程序请求 root 访问权限时,会显示一个来自 SuperSu 的对话框,然后用户单击“授予”以允许权限。

我希望能够绕过这一步,让我的应用程序以编程方式获得 root 权限,而无需单击 SuperSu 对话框上的“授予”按钮。

我该怎么做?

最佳答案

I want to be able to bypass this step and let my app get root permission programmatically without having to click on the "Grant" button on the SuperSu dialog.

你不能。用户必须授予您的应用根访问权限。除非您构建自己的 SU 管理应用程序,否则您无法绕过此初始权限请求。

I'm providing a device with the app, then users use the device (which I own). Just to provide more context, when I release the device it already has the app and it has already been granted root access by SuperSu. So what I'm trying to avoid is a case whereby the app might loose the permission and then ask for it again while its out there in the field. The users are not tech savvy and they will not know what the SuperSu dialog is or what to do with it.

如果您拥有该设备,则可以在 SuperSu 设置中将“默认访问权限”更改为“授予”。您还可以针对每个应用更改此设置。

您在评论中提到您的应用已经拥有 super 用户权限。您拥有 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);
}

我不推荐使用上面的代码。您应该保持透明并教育您的用户。如果您拥有该设备,如您所声称的那样,您可以使用该应用自行更改这些设置。

关于android - 如何在没有 SuperSU 的情况下授予 root 访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42374321/

相关文章:

android - 顶部和底部带有图标的拆分操作栏

android - Android 类 BaseAdapter 中方法 getItem 和 getItemId 的 Intent 是什么?

android - 禁用安装的 Android Market 跟踪

android - eclipse 不显示 Avd 管理器窗口和 android 项目

android - gammu - 在指定的超时时间内没有响应。应该是手机没连接

android - 处理橱柜中列表的最佳方式(android 库)

java - 尝试在 Activity 之间切换,不知道为什么这不起作用?

android - 单击按钮时更改按钮的 drawableLeft

android - Android:在手机上处理视频和声音

java - 从一个txt文件导入数据到android数据库