最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用元數(shù)據(jù)實(shí)現(xiàn)配置信息的傳遞方法詳細(xì)介紹

 更新時(shí)間:2022年09月28日 10:02:58   作者:Shewyoo  
這篇文章主要介紹了Android使用元數(shù)據(jù)實(shí)現(xiàn)配置信息的傳遞方法,也就是實(shí)現(xiàn)配置快捷菜單功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

前序

格式

<meta-data android:name="weather" android:value="xxx"/>

什么場景需要使用?

使用第三方SDK,需要在APP應(yīng)用內(nèi)使用別的APP的整合包,如使用微信登錄、某某地圖等。

一、在代碼中獲取元數(shù)據(jù)

在java代碼中,獲取元數(shù)據(jù)信息的步驟分為下列三步:

  • 調(diào)用getPackageManager方法獲得當(dāng)前應(yīng)用的包管理器;
  • 調(diào)用包管理器的getActivityInfo方法獲得當(dāng)前活動(dòng)的信息對象;
  • 活動(dòng)信息對象的metaData是Bundle包裹類型,調(diào)用包裹對象的getString即可獲得指定名稱的參數(shù)值。

例:從清單文件中獲取元數(shù)據(jù)并顯示到屏幕上

清單文件

        <activity
            android:name=".MetaDataActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="weather" android:value="xxx"/>
        </activity>

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_meta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

java類

public class MetaDataActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meta_data);
        TextView tv_meta = findViewById(R.id.tv_meta);
        //獲取應(yīng)用包管理器
        PackageManager pm = getPackageManager();
            try {
                //從應(yīng)用包管理器中獲取當(dāng)前的活動(dòng)信息
                ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
                //獲取活動(dòng)附加的元數(shù)據(jù)信息
                Bundle bundle = info.metaData;
                String weather = bundle.getString("weather");
                tv_meta.setText(weather);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
    }
}

運(yùn)行結(jié)果

二、給應(yīng)用頁面注冊快捷方式

元數(shù)據(jù)不僅能傳遞簡單的字符串參數(shù),還能傳送更復(fù)雜的資源數(shù)據(jù),如支付寶的快捷式菜單。

利用元數(shù)據(jù)配置快捷菜單

元數(shù)據(jù)的meta-data標(biāo)簽除了前面的name屬性和value屬性,還擁有resource屬性,該屬性可指定一個(gè)XML文件,表示元數(shù)據(jù)想要的復(fù)雜信息保存于XML數(shù)據(jù)之中。

利用元數(shù)據(jù)配置快捷菜單的步驟如下:

  • 在res/values/strings.xml添加各個(gè)菜單項(xiàng)名稱的字符串配置
  • 創(chuàng)建res/xml/shortcuts.xml,在該文件中填入各組菜單項(xiàng)的快捷方式定義。
  • 給activity節(jié)點(diǎn)注冊元數(shù)據(jù)的快捷菜單配置。

例:長按應(yīng)用出現(xiàn)快捷菜單

清單文件AndroidManifest.xml

     <activity
            android:name=".ActStartActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
        </activity>

新建shortcuts.xml文件用于配置快捷菜單

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutLongLabel="@string/first_long"
        android:shortcutShortLabel="@string/first_short">
<!--文字太長則顯示shotLabel ↑-->
<!--點(diǎn)擊選項(xiàng)跳轉(zhuǎn)到的頁面 ↓-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.chapter2"
            android:targetClass="com.example.chapter2.ActStartActivity"/>
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</resources>

運(yùn)行結(jié)果:長按出現(xiàn)快捷菜單

到此這篇關(guān)于Android使用元數(shù)據(jù)實(shí)現(xiàn)配置信息的傳遞方法詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Android傳遞配置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法

    Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法,涉及Android中Listview的響應(yīng)點(diǎn)擊與樣式變換相關(guān)操作技巧,需要的朋友可以參考下
    2015-12-12
  • Action獲取請求參數(shù)的三種方式

    Action獲取請求參數(shù)的三種方式

    這篇文章主要介紹了Action獲取請求參數(shù)的三種方式的,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Flutter數(shù)據(jù)庫的使用方法

    Flutter數(shù)據(jù)庫的使用方法

    這篇文章主要介紹了Flutter數(shù)據(jù)庫的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Android使用Item Swipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能

    Android使用Item Swipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能

    大家都用過QQ,肯定有人好奇QQ滑動(dòng)刪除Item的效果是怎樣實(shí)現(xiàn)的,其實(shí)我們使用Swipemenulistview就可以簡單的實(shí)現(xiàn)。這篇文章主要介紹了Android使用ItemSwipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能,需要的朋友可以參考下
    2017-02-02
  • Android 下調(diào)試手機(jī)截屏的方法

    Android 下調(diào)試手機(jī)截屏的方法

    這篇文章主要介紹了Android 下調(diào)試手機(jī)截屏的方法的相關(guān)資料,希望通過本文大家能掌握這樣的功能,需要的朋友可以參考下
    2017-09-09
  • Android實(shí)現(xiàn)千變?nèi)f化的ViewPager切換動(dòng)畫

    Android實(shí)現(xiàn)千變?nèi)f化的ViewPager切換動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)千變?nèi)f化的ViewPager切換動(dòng)畫,自定義PageTransformer實(shí)現(xiàn)個(gè)性的切換動(dòng)畫,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android開發(fā)之TabActivity用法實(shí)例詳解

    Android開發(fā)之TabActivity用法實(shí)例詳解

    這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法

    android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法

    今天小編就為大家分享一篇關(guān)于android關(guān)于按鈕點(diǎn)擊效果實(shí)現(xiàn)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • android實(shí)現(xiàn)簡單計(jì)算器功能

    android實(shí)現(xiàn)簡單計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android自定義View實(shí)現(xiàn)時(shí)鐘功能

    Android自定義View實(shí)現(xiàn)時(shí)鐘功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)時(shí)鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評論

普安县| 绥江县| 略阳县| 神池县| 原平市| 宝清县| 北京市| 丹凤县| 怀集县| 栖霞市| 常山县| 岢岚县| 黄平县| 濮阳市| 随州市| 永城市| 中江县| 常山县| 佛坪县| 来安县| 教育| 景东| 琼结县| 政和县| 元朗区| 米脂县| 射阳县| 南溪县| 彩票| 镇江市| 潞城市| 麻城市| 荃湾区| 岳阳市| 温泉县| 蓝山县| 都匀市| 维西| 新绛县| 界首市| 招远市|