「Android/meta-data」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→meta-dataの値をactivityで取得する方法) |
|||
| (同じ利用者による、間の8版が非表示) | |||
| 行1: | 行1: | ||
==meta-dataの値をactivityで取得する方法== | ==meta-dataの値をactivityで取得する方法== | ||
| + | meta-dataの名前は"com.example.~"のようにドメインをつけなくても良いが、つけたほうがより良い。(googleのライブラリとかはドメインをつけてる) | ||
| + | |||
-AndroidManifest.xml | -AndroidManifest.xml | ||
| − | + | <application | |
| − | + | ||
| − | + | ||
android:allowBackup="true" | android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | android:icon="@mipmap/ic_launcher" | ||
| 行10: | 行10: | ||
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 | |
| − | </ | + | android:name="com.example.custom1" |
| + | android:value="hogehoge" /> | ||
| + | </application> | ||
-MainActivity.java | -MainActivity.java | ||
| − | Context context = | + | Context context = MainActivity.this.getApplicationContext(); |
| − | + | if (context != null) { | |
| − | ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); | + | 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(); | ||
} | } | ||
| − | |||
| − | |||
} | } | ||
2018年6月13日 (水) 12:09時点における最新版
meta-dataの値をactivityで取得する方法
meta-dataの名前は"com.example.~"のようにドメインをつけなくても良いが、つけたほうがより良い。(googleのライブラリとかはドメインをつけてる)
-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();
}
}
