這篇文章主要介紹了Android5.1系統(tǒng)如何通過包名給應用開放系統(tǒng)權限,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司基于成都重慶香港及美國等地區(qū)分布式IDC機房數(shù)據(jù)中心構建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務器托管報價,主機托管價格性價比高,為金融證券行業(yè)簡陽服務器托管,ai人工智能服務器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業(yè)成都idc公司。
常用的給應用開放系統(tǒng)權限的方法是直接將該應用做成系統(tǒng)應用(即在AndroidManifest.xml文件里加上:android:sharedUserId="android.uid.system"),但是這種做法限制了應用本身的自由,也就是說經過系統(tǒng)簽名后的系統(tǒng)應用只能夠在對應的Android平臺上安裝使用,無法向其他應用一樣放到Android市場里兼容所有的Android設備。
現(xiàn)在此介紹一種通過修改Android平臺系統(tǒng)層代碼,根據(jù)指定的應用包名給對應的應用在該平臺上開放系統(tǒng)權限,這樣既不應用應用的兼容性,又解決了應用想調用一些系統(tǒng)層接口而沒有權限的矛盾。
該方法的核心是:在ActivityManagerService的startProcessLocked
接口中把uid和gid都改為0.
需要開放系統(tǒng)權限的包名:
1、net.forclass.fcstudent
2、com.ckl.launcher
3、com.creative.fcstudent
修改步驟:
1、應用安裝在設備上之后,點擊啟動必定會調用ActivityManagerService的startProcessLocked接口來開啟一個新的進程,而給應用開放系統(tǒng)權限目的其實就是使應用能夠成為超級應用,運行在系統(tǒng)進程中,這樣我們只需要在startProcessLocked接口里面將應用的uid修改為0即可。
ActivityManagerService.java (frameworks\base\services\core\java\com\android\server\am) final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName, boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) { // modified by haming patch begin, configure system permission for some special application. if ("net.forclass.fcstudent".equals(info.packageName) || "com.ckl.launcher".equals(info.packageName) || "com.creative.fcstudent".equals(info.packageName) || "com.hampoo.hampoointerfacetestdemo".equals(info.packageName)){ info.uid = 0; } // modified by haming patch end. long startTime = SystemClock.elapsedRealtime(); ProcessRecord app; ...... // 此處省略好多行 checkTime(startTime, "startProcess: stepping in to startProcess"); startProcessLocked( // 再次調用startProcessLocked重載方法 app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); checkTime(startTime, "startProcess: done starting proc!"); return (app.pid != 0) ? app : null; }在重載方法startProcessLocked(ProcessRecord app, String hostingType,String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs)里面將應用的gid也改為0: private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) { long startTime = SystemClock.elapsedRealtime(); ...... // 此處省略好多行 try { int uid = app.uid; int[] gids = null; int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; if (!app.isolated) { ...... // 此處省略好多行 // modified by haming patch begin, configure system permission for some special application. if ("net.forclass.fcstudent".equals(app.info.packageName) || "com.ckl.launcher".equals(app.info.packageName) || "com.creative.fcstudent".equals(app.info.packageName) || "com.hampoo.hampoointerfacetestdemo".equals(app.info.packageName)){ SystemProperties.set("sys.permission.enable", "true"); // 設置一個系統(tǒng)屬性,在Zygote進行判斷是否拋出異常 gids[0] = 0; gids[1] = 0; } else { gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); gids[1] = UserHandle.getUserGid(UserHandle.getUserId(uid)); } // modified by haming patch end. } ...... // 此處省略好多行 } catch (RuntimeException e) { // XXX do better error recovery. app.setPid(0); mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid); if (app.isolated) { mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid); } Slog.e(TAG, "Failure starting process " + app.processName, e); } }
2、ZygoteConnection.java里的applyUidSecurityPolicy(Arguments args, Credentials peer, String peerSecurityContext)接口會對進程id進行判斷,如果小于Process.SYSTEM_UID(1000)則認為是非法,而zygote是具有root權限的唯一server,所有在判斷之后就可以通過讀取前面設定的系統(tǒng)屬性“sys.permission.enable”的值來決定是否拋出異常。
ZygoteConnection.java (frameworks\base\core\java\com\android\internal\os) private static void applyUidSecurityPolicy(Arguments args, Credentials peer, String peerSecurityContext) throws ZygoteSecurityException { int peerUid = peer.getUid(); if (peerUid == 0) { // Root can do what it wants } else if (peerUid == Process.SYSTEM_UID ) { // System UID is restricted, except in factory test mode String factoryTest = SystemProperties.get("ro.factorytest"); boolean uidRestricted; /* In normal operation, SYSTEM_UID can only specify a restricted * set of UIDs. In factory test mode, SYSTEM_UID may specify any uid. */ uidRestricted = !(factoryTest.equals("1") || factoryTest.equals("2")); // modified by haming patch begin, configure system permission for some special application. if (uidRestricted && args.uidSpecified && (args.uid < Process.SYSTEM_UID)) { if (!SystemProperties.getBoolean("sys.permission.enable", false)){ throw new ZygoteSecurityException( "System UID may not launch process with UID < " + Process.SYSTEM_UID); } else { SystemProperties.set("sys.permission.enable", "false"); } } // modified by haming patch end. } else { // Everything else if (args.uidSpecified || args.gidSpecified || args.gids != null) { throw new ZygoteSecurityException( "App UIDs may not specify uid's or gid's"); } } ...... // 此處省略好多行 }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android5.1系統(tǒng)如何通過包名給應用開放系統(tǒng)權限”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!