facebook twitter hatena line email

Android/meta-data

提供: 初心者エンジニアの簡易メモ
2017年3月24日 (金) 20:29時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

meta-dataの値をactivityで取得する方法

-AndroidManifest.xml

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@style/AppTheme" >
       <meta-data
           android:name="log_flag"
           android:value="true" />
       <meta-data
           android:name="custom1"
           android:value="hogehoge" />
   </application>

-MainActivity.java

Context context = (Context)MainActivity.this;
try {
    ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    if (appInfo.metaData != null) {
        Log.i("test", "log_flag=" + appInfo.metaData.getBoolean("log_flag", false)); // true
        Log.i("test", "custom1=" + appInfo.metaData.getString("custom1")); // hogehoge
    }
} catch (PackageManager.NameNotFoundException e) {
   e.printStackTrace();
}