android 擷取application和activity下meta-data中的值

來源:互聯網
上載者:User

標籤:android   meta-data   application   activityinfo   applicationinfo   

        meta-data在AndroidManifest中是以鍵值對的形式存在的,可以將meta-data放在application根節點下,也可以放在某個activity節點下。因為存放的位置不同,因此擷取value時也要使用對應的方法,下面是我寫的一個小測試。

代碼如下:

先是在AndroidManifest中的application和mainActivity下添加兩個meta-data屬性。

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <meta-data            android:name="com.lzy.test"            android:value="測試" />        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <meta-data                android:name="com.example.metatest.MainActivity.com.lzy.activity.test"                android:value="Activity測試" />            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

接下來在mainActivity中來擷取這兩個屬性的值。

package com.example.metatest;import android.app.Activity;import android.content.ComponentName;import android.content.pm.ActivityInfo;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.util.Log;import android.widget.Toast;/** * meta 測試 *  * @author Administrator *  */public class MainActivity extends Activity {public static final String TEST = "com.lzy.test";public static final String ACTIVITYTEST = MainActivity.class.getName()+".com.lzy.activity.test";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String test = getMetaDataStringApplication(TEST, null);String activityTest = getMetaDataStringFromActivity(ACTIVITYTEST, null);Toast.makeText(this, test+"--"+activityTest, Toast.LENGTH_LONG).show();}/** * 根據key從Application中返回的Bundle中擷取value *  * @param key * @param defValue * @return */private String getMetaDataStringApplication(String key, String defValue) {Bundle bundle = getAppMetaDataBundle(getPackageManager(), getPackageName());if (bundle != null && bundle.containsKey(key)) {return bundle.getString(key);}return defValue;}/** * 擷取Application中的meta-data. *  * @param packageManager * @param packageName * @return */private Bundle getAppMetaDataBundle(PackageManager packageManager,String packageName) {Bundle bundle = null;try {ApplicationInfo ai = packageManager.getApplicationInfo(packageName,PackageManager.GET_META_DATA);bundle = ai.metaData;} catch (NameNotFoundException e) {Log.e("getMetaDataBundle", e.getMessage(), e);}return bundle;}/** * 根據key從Activity中返回的Bundle中擷取value *  * @param key * @param defValue * @return */private String getMetaDataStringFromActivity(String key, String defValue) {Bundle bundle = getActivityMetaDataBundle(getPackageManager(), getComponentName());if (bundle != null && bundle.containsKey(key)) {return bundle.getString(key);}return defValue;}/** * 擷取Activity中的meta-data. *  * @param packageManager * @param component * @return */private Bundle getActivityMetaDataBundle(PackageManager packageManager, ComponentName component) {Bundle bundle = null;try {ActivityInfo ai = packageManager.getActivityInfo(component,PackageManager.GET_META_DATA);bundle = ai.metaData;} catch (NameNotFoundException e) {Log.e("getMetaDataBundle", e.getMessage(), e);}return bundle;}}

源碼下載

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

android 擷取application和activity下meta-data中的值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.