獲取meta data的時候,正常的非純數字的字符串,設置在value裏面,用getString獲取是正常的。android
可是若是含有純數字的字符串,用getString獲取返回爲null。也許你會想到用getInt,getLong獲取,很抱歉,也多是null。app
解決方式有兩種:xml
一.資源
繼續使用value="1234567890",可是在純數字字符串開頭加上"\ "(反斜槓+空格)這樣系統會自動讀取爲字符串而不是其餘格式,形如字符串
<meta-data android:name="appkey" android:value="\ 1234567890" />
二.使用resources屬性經過getInt獲取資源id,再獲取資源id對應的值,這種方式可擴展性很強,能夠獲取全部資源而不僅是string,形如get
<meta-data android:name="appkey" android:resource="@string/AppKey" />
在strings.xml中定義AppKey的值爲string
<string name="AppKey">1234567890</string>
三.Application下獲取meta資源的方式io
public static String getStringMetaData(String name) { int valueId = 0; try { ApplicationInfo appInfo = getApplicationContext().getPackageManager() .getApplicationInfo(getApplicationContext().getPackageName(), PackageManager.GET_META_DATA); valueId = appInfo.metaData.getInt(name); if (valueId != 0) { return getApplicationContext().getResources().getString(valueId); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return ""; }