2017-02-21 205 views
0

我目前能夠使用SuperSU在根設備上的應用程序中獲得root訪問權限。當我的應用程序請求超級用戶訪問權限時,會顯示一個用戶點擊「授予」以允許權限的SuperSu對話框。如何在沒有SuperSU的情況下授予root訪問權限

我想能夠繞過這一步,讓我的應用程序獲得root權限programmaticaly,而不必點擊SuperSu對話框上的「授予」按鈕。

我該如何解決這個問題?

+0

嗡嗡聲,是不是因爲一個非常具體的原因,用戶應明確授予該權限? – lelloman

+0

是的,理想情況下,用戶應該授予該權限。在這種情況下,我想要自動授予權限,因爲SuperSU可以做到這一點,那麼我的應用程序也應該能夠做到這一點? – kev

+0

我的意思是,如果用戶需要明確授予他的權限,自動獲得該權限可能被視爲惡意移動,這是因爲他應該知道您的應用程序需要root權限。或者我只是太可疑... – lelloman

回答

4

我希望能夠繞過這一步,讓我的應用程序以編程方式獲得root權限,而無需點擊SuperSu對話框上的「授予」按鈕。

你不行。用戶必須授予您的應用程序根權限。除非您構建自己的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); 
} 

我不建議使用上面的代碼。你應該透明並教育你的用戶。如果您擁有該設備,就像您聲稱的那樣,您可以使用該應用自行更改這些設置。

相關問題