Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> AndroidManifest.xml中使用Meta-Data

AndroidManifest.xml中使用Meta-Data

編輯:初級開發

Sometimes you have the need to set up some app-wide configuration information in an android app or need to create a class that can be used in multiple projects with a generic way of setting configuration values. This is particularly useful for things like API keys that will probably be different across aPPS but should be Accessible in the same way. There are several ways to do it, but the one I’ve come to prefer is adding a meta-data node to the androidManifest.XML file.

This fIEld can be used to store a boolean, float, int, or String and is later Accessed by the Bundle method for your data type (e.g., getInt()). Here is an example of how to define a value in your androidManifest.XML:

  1. <?XML version="1.0" encoding="utf-8"?>
     
  2. <manifest XMLns:android="http://schemas.android.com/apk/res/android"
     
  3.   package="com.example.readmetadata"
     
  4.   android:versionCode="1"
     
  5.   android:versionName="1.0">
     
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
     
  7.         <activity android:name=".MainMenu" android:label="@string/app_name">
     
  8.             <intent-filter>
     
  9.                 <action android:name="android.intent.action.MAIN" />
     
  10.                 <category android:name="android.intent.category.LAUNCHER" />
     
  11.             </intent-filter>
     
  12.         </activity>
     
  13.         <meta-data android:name="my_api_key" android:value="mykey123" />
     
  14.     </application>
     
  15.     <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
     
  16. </manifest>

Reading this meta-data takes just a few lines of Java:

  1. try {
     
  2.     ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
     
  3.     Bundle bundle = ai.metaData;
     
  4.     String myApiKey = bundle.getString("my_api_key");
     
  5. } catch (NameNotFoundException e) {
     
  6.     Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
     
  7. } catch (NullPointerException e) {
     
  8.     Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());
     
  9. }

Activity extends ContextWrapper which has a getPackageManager() method. That method returns the PackageManager, which is used to fetch the ApplicationInfo, passing the package name and the meta-data flag. The returned ApplicationInfo contains a fIEld, metaData, which is actually a Bundle containing all the meta data. Line 4 fetches a String that is the same as the “android:name” parameter in the XML.

That sounds more complicated than it really is. Basically, if you have an Activity, you can fetch the Bundle that has all your meta-data from the androidManifest and use it throughout the app.

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved