「Android/meta-data」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→meta-dataの値をactivityで取得する方法) |
(→meta-dataの値をactivityで取得する方法) |
||
| 行8: | 行8: | ||
android:theme="@style/AppTheme" > | android:theme="@style/AppTheme" > | ||
<meta-data | <meta-data | ||
| − | android:name="log_flag" | + | android:name="com.example.log_flag" |
android:value="true" /> | android:value="true" /> | ||
<meta-data | <meta-data | ||
| − | android:name="custom1" | + | android:name="com.example.custom1" |
android:value="hogehoge" /> | android:value="hogehoge" /> | ||
</application> | </application> | ||
| 行21: | 行21: | ||
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); | ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); | ||
if (appInfo.metaData != null) { | if (appInfo.metaData != null) { | ||
| − | if (appInfo.metaData.get("log_flag") != null) { | + | if (appInfo.metaData.get("com.example.log_flag") != null) { |
| − | Log.i("test", "log_flag=" + appInfo.metaData.getBoolean("log_flag", false)); // true | + | Log.i("test", "log_flag=" + appInfo.metaData.getBoolean("com.example.log_flag", false)); // true |
} | } | ||
| − | if (appInfo.metaData.get("custom1") != null) { | + | if (appInfo.metaData.get("com.example.custom1") != null) { |
| − | Log.i("test", "custom1=" + appInfo.metaData.getString("custom1")); // hogehoge | + | Log.i("test", "custom1=" + appInfo.metaData.getString("com.example.custom1")); // hogehoge |
} | } | ||
} | } | ||
2017年5月30日 (火) 14:05時点における版
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="com.example.log_flag"
android:value="true" />
<meta-data
android:name="com.example.custom1"
android:value="hogehoge" />
</application>
-MainActivity.java
Context context = MainActivity.this.getApplicationContext();
if (context != null) {
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData != null) {
if (appInfo.metaData.get("com.example.log_flag") != null) {
Log.i("test", "log_flag=" + appInfo.metaData.getBoolean("com.example.log_flag", false)); // true
}
if (appInfo.metaData.get("com.example.custom1") != null) {
Log.i("test", "custom1=" + appInfo.metaData.getString("com.example.custom1")); // hogehoge
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
