android使用獲取所有應用的資源id和uri

2021-07-07 10:03:28 字數 3596 閱讀 3020

摘要

android使用獲取所有應用的資源id和uri

目錄[-]

在某些應用中,為了實現應用apk資源放入重複利用,或者使用反射得到本應用的資源,需要使用反射反射方式獲得,但resources類中也自帶了這種獲取方式,並且功能更加強大

你可以獲取string,color,drawable,raw,xml等檔案,因此也就意味著,這裡可以獲取的資源是res中已定義的資源,對於控制項id的獲取,暫時無法做到

android.content.res.resources.class

?

1

2

3

4

5

6

7

8

9

10

11

publicintgetidentifier(string name, string deftype, string defpackage)

trycatch(exception e)

returnmassets.getresourceidentifier(name, deftype, defpackage);

}

一.獲取資源的id

1.如下,我們可以獲取當前應用的資源id

?

1

2

intdrawableid = mcontext.getresources().getidentifier("ic_launcher","drawable", mcontext.getpackagename());

mimageview.setimageresource(drawableid);

2.我們也可以獲取其他應用的資源id

?

1

2

3

4

5

resources resources = context.getresources();

intindentify= getresources().getidentifier("icon","drawable","org.anddev.android.testproject");

intresid = getresources().getidentifier("background","color", getpackagename());

startbtn.settextcolor(getresources().getcolor(resid));

對於這種方式,我們也可以這麼做

?

1

intindentify = getresources().getidentifier(org.loveandroid.androidtest:drawable/icon",null,null);

3.進行封裝一下

?

1

2

3

4

5

6

7

8

9

10

11

12

publicstaticintgetresourceid(context context,string name,string type,string packagename)catch(namenotfoundexception e)

return0;

}

二.獲取資源的uri

android系統中,應用的資源儲存時也通常會被存入 資料庫,也可以被共享,因此來說資源會獲得應用的uri

?

1

2

uri uri = uri.parse("android.resource://"+getpackagename()+"/"+r.raw.xinyueshenhua);

uri uri = uri.parse("android.resource://"+getpackagename()+"/"+r.drawable.ic_launcher);

我們也可以進一步封裝

?

1

2

3

4

publicstaticuri getresourceuri(intresid,string packagename)

三.獲取系統資源

?

1

intindentify = getresources().getidentifier("actionbar_bg","drawable","android");//注意,最後乙個引數必須是「android」

Android 獲取應用簽名

android studio中獲取應用簽名通過在as的terminal中,keystore路徑下執行 1 debug版本 android studio會預設生成乙個debug.keystore簽名檔案,它的預設位置在 home android debug.keystore 2 release版本 首...

獲取所有已安裝的應用

1.獲取packagemanager packagemanager manager getcontext getpackagemanager 2.獲取已安裝的應用 listpackageinfos manager.getinstalledpackages 0 3.遍歷 packageinfo列表,獲...

獲取Android手機中所有的簡訊

首先加上讀取資訊的許可權 public string getsmsinphone uri uri uri.parse sms uri all cursor cur cr.query uri,projection,null,null,date desc if cur.movetofirst else ...