我试图将一个字符串放入清单并检索它。不幸的是它不起作用。它不打印任何东西而不是“hieutot”。如何解决?
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("HieuND", "TESSSSSSSSSSSS");
setApplicationId(this, "hieutot");
final String s = getMetadataApplicationId(this);
Log.d("HieuND", s);
}
public static void setApplicationId(final Context context, final String applicationId) {
try {
final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA);
if (ai.metaData != null) {
ai.metaData.putString("test", applicationId);
}
} catch (final PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}
public static String getMetadataApplicationId(final Context context) {
try {
final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA);
if (ai.metaData != null) {
final String s = ai.metaData.getString("test");
Log.d("HieuND", "zz = " + s);
return s;
}
} catch (final PackageManager.NameNotFoundException e) {
// if we can't find it in the manifest, just return null
}
return null;
}
清单
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="test" android:value=""/>
</application>