Android添加用戶(hù)組及自定義App權(quán)限的方法
Android:4.4.4
一、應(yīng)用場(chǎng)景
在Android設(shè)備上,現(xiàn)在我們外接了一個(gè)USB轉(zhuǎn)串口的設(shè)備,設(shè)備節(jié)點(diǎn)是/dev/ttyUSB0:
# ls -l /dev/ttyUSB0 crw-rw---- 1 root root 188, 0 /dev/ttyUSB0
信息顯示:該設(shè)備的用戶(hù)及其所屬組別都是root,root的持有者對(duì)該設(shè)備具有讀寫(xiě)權(quán)限。但是,我們的App是被排除在root之外的,總之無(wú)法讀寫(xiě)該設(shè)備。
一個(gè)解決方案是:賦予others以讀寫(xiě)權(quán)限。但是這樣,任何其他第三方應(yīng)用都可以訪(fǎng)問(wèn)我們的設(shè)備,安全性一絲不剩。
另一個(gè)解決方案就是:改變/dev/ttyUSB0所屬的用戶(hù)組:
crw-rw---- 1 root selfgroup 188, 0 /dev/ttyUSB0
哪個(gè)App想使用該設(shè)備,就把它加入到selfgroup 組中。由于權(quán)限分發(fā)權(quán)在我們手中,安全性就得到了保障。
二、添加自定義用戶(hù)組
接下來(lái)就是怎么添加我們自己的用戶(hù)組。
在Android中,每一個(gè)用戶(hù)組都有一個(gè)唯一的ID號(hào),定義在文件:
system\core\include\private\android_filesystem_config.h /* This is the master Users and Groups config for the platform. * DO NOT EVER RENUMBER */ #define AID_ROOT 0 /* traditional unix root user */ #define AID_SYSTEM 1000 /* system server */ #define AID_RADIO 1001 /* telephony subsystem, RIL */ #define AID_BLUETOOTH 1002 /* bluetooth subsystem */ #define AID_GRAPHICS 1003 /* graphics devices */ #define AID_INPUT 1004 /* input devices */ #define AID_AUDIO 1005 /* audio devices */ #define AID_CAMERA 1006 /* camera devices */
仿照其添加自己的ID號(hào)(不允許重復(fù))、并賦予它一個(gè)字符串的名字(后文用到):
/** 第1步 */ #define AID_SELF_GROUP 8011 /** 第2步 */ static const struct android_id_info android_ids[] = { { "root", AID_ROOT, }, {省略 .......................}, /** 自定義組名 */ {{ "selfgroup", AID_SELF_GROUP, },}, }
然后在Android源碼/device目錄下板級(jí).rc文件中:
編譯boot.img并燒錄,重啟后查看節(jié)點(diǎn)組別已經(jīng)變成自定義的“selfgroup”。
三、為自定義組別添加權(quán)限管理
設(shè)備中App要訪(fǎng)問(wèn)我們的設(shè)備,需要加入“selfgroup”組中。所以我們需要提供權(quán)限信息給App,使得App:
<uses-permission android:name="android.permission.SELFGROUP" />
即可加入到“selfgroup”組中。
這需要做兩步。
1、組控制權(quán)限的實(shí)現(xiàn):
Android源碼/frameworks/base/data/etc/platform.xml <permission name="android.permission.SELFGROUP" > <group gid="selfgroup" /> </permission>
2、組控制權(quán)限的聲明:
Android源碼/frameworks/base/core/res/AndroidManifest.xml <permission android:name="android.permission.SELFGROUP" android:permissionGroup="android.permission-group.SELFGROUP" android:protectionLevel="dangerous" android:label="@string/permlab_sys_selfgroup" android:description="@string/permdesc_sys_selfgroup" />
自此,權(quán)限定義完成。
另,如何判斷App申請(qǐng)組權(quán)限成功與否?
Android源碼/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java public final class ActivityManagerService extends ActivityManagerNative { static final String TAG = "ActivityManager"; private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr) { int uid = app.uid; int[] gids = null; StringBuilder buf = mStringBuilder; buf.setLength(0); buf.append("Start proc "); buf.append(app.processName); buf.append(" for "); buf.append(hostingType); if (hostingNameStr != null) { buf.append(" "); buf.append(hostingNameStr); } buf.append(": pid="); buf.append(startResult.pid); buf.append(" uid="); buf.append(uid); buf.append(" gids={"); if (gids != null) { for (int gi=0; gi<gids.length; gi++) { if (gi != 0) buf.append(", "); buf.append(gids[gi]); } } buf.append("}"); Slog.i(TAG, buf.toString()); } }
即:當(dāng)啟動(dòng)App的時(shí)候,ActityManagerService會(huì)把該App的Uid、Gid等信息輸出到控制臺(tái),我們通過(guò)log查看“gids=”字段是否含有我們自定義的組ID號(hào)“8011”即可。
比如啟動(dòng)微信Log:
I/ActivityManager( 977): Start proc com.tencent.mm for activity com.tencent.mm/.ui.LauncherUI: pid=11060 uid=10258 gids={50258, 3003, 1028, 1015, 1023, 3002, 3001}
以上這篇Android添加用戶(hù)組及自定義App權(quán)限的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android系統(tǒng)添加Linux驅(qū)動(dòng)
- Android實(shí)現(xiàn)EditText添加下劃線(xiàn)
- Android中多行文本末尾添加圖片排版問(wèn)題的解決方法
- Android添加指紋解鎖功能的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)添加商品到購(gòu)物車(chē)動(dòng)畫(huà)效果
- Android GridView仿微信添加多圖效果
- Android中recyclerView底部添加透明漸變效果
- Android GridView添加頭部問(wèn)題的解決
- 詳解如何在Android Studio中添加RecyclerView-v7支持包
- Android百度地圖添加Marker失真問(wèn)題的解決方案
- Android手機(jī)屏幕同步工具asm.jar
- Android系統(tǒng)工具類(lèi)詳解
- Android中APK簽名工具之jarsigner和apksigner詳解
- 超實(shí)用的android網(wǎng)絡(luò)工具類(lèi)
- Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類(lèi)
- Android系統(tǒng)添加自己寫(xiě)的工具
相關(guān)文章
Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
本篇文章主要介紹了Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
這篇文章主要介紹了Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
這篇文章主要為大家詳細(xì)介紹了android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android開(kāi)發(fā)中setContentView和inflate的區(qū)別分析
這篇文章主要介紹了Android開(kāi)發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下2016-07-07關(guān)于Android中ListView嵌套GridView的問(wèn)題
在Android開(kāi)發(fā)的過(guò)程中可能需要用到listview嵌套gridview的場(chǎng)景,但是在嵌套過(guò)程中也許會(huì)遇到問(wèn)題,我們下面一起來(lái)看看是什么問(wèn)題以及如何解決。2016-08-08Android自定義ImageView實(shí)現(xiàn)自動(dòng)放大縮小動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)自動(dòng)放大縮小動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06