日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Intent进阶 和 Intent-filter 学习笔记

發(fā)布時間:2025/7/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Intent进阶 和 Intent-filter 学习笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1,Intent的基礎(chǔ)用法

Intent是android中各activity之間通信的一個很重要的類,一般我們是這么使用的

?
1 //創(chuàng)建一個intent
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Intent intent = new Intent(); ?? //壓值進intent中 //intent是基于一種基于Map的數(shù)據(jù)結(jié)構(gòu) //傳我們的基本數(shù)據(jù)類型的可以之間key,value方式傳值 intent.putExtra("hello","Hello world"); ?? //但是,傳一個對象的時要注意,該對象要實現(xiàn)序列化窗口才可以傳值 //putExtra(String name, Serializable value) Demo demo = new Demo(); intent.putExtra("Demo",demo); ?? //然后,我們把這個intent傳到另外一個activity intent.setClass(xxx.this,zzz.class); startActivity(intent); ?? //------- //目標activity //獲取傳過來的intent Intent intent = getIntent(); ?? //從Intent中獲取值 String hello = intent.getStringExtra("Demo"); Demo demo = (Demo)intent.getSerializable("Demo");
?
1 以上代碼就是最基礎(chǔ)的Intent的用法

2,深入Intent的構(gòu)造和Intent-filter的基礎(chǔ)入門

首先我們看下Intent的構(gòu)造方法

?

?
1 2 3 4 5 6 7 8 9 10 11 12 ????Intent() //Create an empty intent. ????Intent(Intent o) //Copy constructor. ????Intent(String action) //Create an intent with a given action. ????Intent(String action, Uri uri) //Create an intent with a given action and for a given data url. ????Intent(Context packageContext, Class<?> cls) //Create an intent for a specific component. ????Intent(String action, Uri uri, Context packageContext, Class<?> cls) //Create an intent for a specific component with a specified action and data.
?
1 出這些構(gòu)造方法中我們,可以看出,新建一個Intent其實可以設(shè)置很多東西,這里我說說componentName,action,category,data

componentName

componentName 直譯的話就是組件的名字的意思,如果我們寫的一個類當作成一個組件,那么這個componentName就是用來封裝我們寫的這些類的位置.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 //創(chuàng)建一個component的對象 ComponentName componentName = new ComponentName(Context.this, xxx.class); ?? Intent intent? = new Intent(); Intent.setComponent(componentName); ?? startActivity(intent); ?? //---------- //在xxxactivity中 ComponentName comp = getIntent().getComponent(); ?? String packageName = comp.getPackName(); String className = comp.getClassName();
?
1 其實細心的同學可以發(fā)現(xiàn),其實跟setClass(),沒什么區(qū)別嗎,而且,還比setClass()麻煩,其實,setClass()是封裝了這個功能而已,最實現(xiàn)還是要用到這個類.

Action,category,data簡單入門

我感覺,學intent 和 intent-filter把action,category,data搞清楚才算真正的掌握了intent的用法.

Action

如果學過Structs2的同學應該不會陌生,所謂的action就是發(fā)送一個特定的請求,然后,由一個符合這個請求的activity響應

官方文檔中是這么說action的:

The action largely determines how the rest of the intent is structured — particularly the data and extras fields — much as a method name determines a set of arguments and a return value. For this reason, it's a good idea to use action names that are as specific as possible, and to couple them tightly to the other fields of the intent. In other words, instead of defining an action in isolation, define an entire protocol for the Intent objects your components can handle.

我簡要說下我理解的大義,有錯歡迎指出

action 在很大一部分程度上決定你的intent 是如何構(gòu)建的,特別是還有data,和 額外的一些字段,還有更多的是如何決定方法名,設(shè)置數(shù)組和返回的參數(shù).由于這個原因,這是一個很好的方法去盡可能的具體的使用action,用action把這些字段緊密的聯(lián)系在一起.另外,靈活定義你的action,然后把它定義在一個文檔中為你的intent對象的組建能夠正常處理

渣翻譯…可能比谷歌翻譯還有爛…有錯歡迎指出!!!!!!!!!!!!!

category

要使一個action能夠正常運行,category是必不可少的,關(guān)于category詳細介紹請看開發(fā)者文檔中的intent and intent-filter

這里說下注意的細節(jié)和使用.

首先,這句話是很重要的..取之于官方文檔

Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters.

這句話,告訴我們在intent-filter(可以用xml創(chuàng)建也可以用代碼創(chuàng)建,這里主要講xml的創(chuàng)建),必須添加一個android.intent.category.DEFAULT,至于,不添加會發(fā)生什么事…我花了半個小時的排錯告訴我是無法運行的

?
1 2 3 4 5 6 7 //在我們要運行的activity中添加 <activity android:name=".SecondActivity" android:label="second"> ?????????????? ????????????<intent-filter > ?????????????? ????????????????<action android:name="kg.tom.TEST"/> ????????????????<category android:name="
android.intent.category.DEFAULT ?
1 2 3 " /> ????????????</intent-filter> ????????</activity>
?
1 2 3 4 5 6 7 //firstActivity //在創(chuàng)建好的一個監(jiān)聽器里面方法中寫上 Intent intent = new Intent(); intent.setAction("kg.tom.TEST"); ?? //利用action來運行相應的activity startActivity(intent);

?

注意:每個intent只能設(shè)置一個action,但是,可以設(shè)置多個category,這里的組合問題,自己好好想想吧.

data

因為就是一些參數(shù)的介紹,看官方文檔就好了,我是這樣理解這個DATA,例如,我們一個Mp3播放器,當我們在任務管理器中點擊一個MP3文件,MP3這個文件就是一個data,就會觸發(fā)我們,在intent-filter匹配這個Mp3,<data />的activity運行..

?

?

請注意!!!這里給的都是些關(guān)鍵代碼片段…

總結(jié)

以上是生活随笔為你收集整理的Intent进阶 和 Intent-filter 学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。