android如何獲取APP的唯一標(biāo)識applicationId

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

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供諸城網(wǎng)站建設(shè)、諸城做網(wǎng)站、諸城網(wǎng)站設(shè)計、諸城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、諸城企業(yè)網(wǎng)站模板建站服務(wù),10年諸城做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

使用getIdentifier()方法可以方便的獲各應(yīng)用包下的指定資源ID。

方式一

int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null);

第一個參數(shù)格式是:包名 + : +資源文件夾名 + / +資源名;是這種格式 然后其他的可以為null

方式二

intindentify= getResources().getIdentifier(“icon”, “drawable”, “com.test.demo”);

第一個參數(shù)為ID名,第二個為資源屬性是ID或者是Drawable,第三個為包名。

示例代碼:

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());
  }
  // 通過反射實現(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),主要使用于移動設(shè)備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

看完了這篇文章,相信你對“android如何獲取APP的唯一標(biāo)識applicationId”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前名稱:android如何獲取APP的唯一標(biāo)識applicationId
本文來源:http://muchs.cn/article30/jpgcso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)網(wǎng)站建設(shè)、動態(tài)網(wǎng)站定制網(wǎng)站、網(wǎng)站維護、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司