這篇文章主要介紹了Android5.1系統(tǒng)如何通過包名給應(yīng)用開放系統(tǒng)權(quán)限,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)簡(jiǎn)陽服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
常用的給應(yīng)用開放系統(tǒng)權(quán)限的方法是直接將該應(yīng)用做成系統(tǒng)應(yīng)用(即在AndroidManifest.xml文件里加上:android:sharedUserId="android.uid.system"),但是這種做法限制了應(yīng)用本身的自由,也就是說經(jīng)過系統(tǒng)簽名后的系統(tǒng)應(yīng)用只能夠在對(duì)應(yīng)的Android平臺(tái)上安裝使用,無法向其他應(yīng)用一樣放到Android市場(chǎng)里兼容所有的Android設(shè)備。
現(xiàn)在此介紹一種通過修改Android平臺(tái)系統(tǒng)層代碼,根據(jù)指定的應(yīng)用包名給對(duì)應(yīng)的應(yīng)用在該平臺(tái)上開放系統(tǒng)權(quán)限,這樣既不應(yīng)用應(yīng)用的兼容性,又解決了應(yīng)用想調(diào)用一些系統(tǒng)層接口而沒有權(quán)限的矛盾。
該方法的核心是:在ActivityManagerService的startProcessLocked
接口中把uid和gid都改為0.
需要開放系統(tǒng)權(quán)限的包名:
1、net.forclass.fcstudent
2、com.ckl.launcher
3、com.creative.fcstudent
修改步驟:
1、應(yīng)用安裝在設(shè)備上之后,點(diǎn)擊啟動(dòng)必定會(huì)調(diào)用ActivityManagerService的startProcessLocked接口來開啟一個(gè)新的進(jìn)程,而給應(yīng)用開放系統(tǒng)權(quán)限目的其實(shí)就是使應(yīng)用能夠成為超級(jí)應(yīng)用,運(yùn)行在系統(tǒng)進(jìn)程中,這樣我們只需要在startProcessLocked接口里面將應(yīng)用的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( // 再次調(diào)用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)里面將應(yīng)用的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"); // 設(shè)置一個(gè)系統(tǒng)屬性,在Zygote進(jìn)行判斷是否拋出異常 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)接口會(huì)對(duì)進(jìn)程id進(jìn)行判斷,如果小于Process.SYSTEM_UID(1000)則認(rèn)為是非法,而zygote是具有root權(quán)限的唯一server,所有在判斷之后就可以通過讀取前面設(shè)定的系統(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"); } } ...... // 此處省略好多行 }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android5.1系統(tǒng)如何通過包名給應(yīng)用開放系統(tǒng)權(quán)限”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
分享名稱:Android5.1系統(tǒng)如何通過包名給應(yīng)用開放系統(tǒng)權(quán)限
網(wǎng)頁路徑:http://www.rwnh.cn/article14/jjshde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、企業(yè)建站、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)