小編給大家分享一下android如何獲取APP的唯一標(biāo)識(shí)applicationId,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

使用getIdentifier()方法可以方便的獲各應(yīng)用包下的指定資源ID。
方式一
int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null);
第一個(gè)參數(shù)格式是:包名 + : +資源文件夾名 + / +資源名;是這種格式 然后其他的可以為null
方式二
intindentify= getResources().getIdentifier(“icon”, “drawable”, “com.test.demo”);
第一個(gè)參數(shù)為ID名,第二個(gè)為資源屬性是ID或者是Drawable,第三個(gè)為包名。
示例代碼:
import java.lang.reflect.Field;
import android.content.Context;
public class ResourceUtil {
private static Context sContext;
public static void init(Context context) {
if (context != null)
sContext = context;
}
public static int getLayoutId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "layout", sContext.getPackageName());
}
public static int getStringId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "string",
sContext.getPackageName());
}
public static int getDrawableId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "drawable", sContext.getPackageName());
}
public static int getStyleId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "style",
sContext.getPackageName());
}
public static int getId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "id",
sContext.getPackageName());
}
public static int getColorId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "color", sContext.getPackageName());
}
public static int getDimenId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "dimen",
sContext.getPackageName());
}
public static int getAnimId(String paramString) {
if (sContext == null)
return 0;
return sContext.getResources().getIdentifier(paramString, "anim", sContext.getPackageName());
}
// 通過反射實(shí)現(xiàn)
public static final int[] getStyleableIntArray(String name) {
try {
if (sContext == null)
return null;
Field field = Class.forName(sContext.getPackageName() + ".R$styleable").getDeclaredField(name);
int[] ret = (int[]) field.get(null);
return ret;
} catch (Throwable t) {
}
return null;
}
public static final int getStyleableIntArrayIndex(String name) {
try {
if (sContext == null)
return 0;
// use reflection to access the resource class
Field field = Class.forName(sContext.getPackageName() + ".R$styleable").getDeclaredField(name);
int ret = (Integer) field.get(null);
return ret;
} catch (Throwable t) {
}
return 0;
}
}Android是什么Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完了這篇文章,相信你對“android如何獲取APP的唯一標(biāo)識(shí)applicationId”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
名稱欄目:android如何獲取APP的唯一標(biāo)識(shí)applicationId-創(chuàng)新互聯(lián)
文章起源:http://www.chinadenli.net/article0/ccsioo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、微信小程序、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)