日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Intent七大属性

發(fā)布時間:2023/11/27 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Intent七大属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、Intent的作用是什么?

???1、Intent?用于封裝程序的”調(diào)用意圖“。兩個Activity之間,可以把需要交換的數(shù)據(jù)封裝成Bundle對象,然后使用Intent攜帶Bundle對象,實現(xiàn)兩個Activity之間的數(shù)據(jù)交換; ? ?2、Intent還是各種應(yīng)用程序組件之間通信的重要媒介。不管想啟動一個Service、Acitivity還是BroadcastReceiver,Android均使用統(tǒng)一的Intent對象來封裝這種”啟動意圖“。很明顯使用Intent提供了一致的編程模型; ? ?3、Intent還有一個好處,如果應(yīng)用程序只是想啟動具有某種特征的組件,并不想和某個具體的組件耦合,則可以通過在intent-filter中配置相應(yīng)的屬性進(jìn)行處理,與stucts2中的MVC框架思路類似。 ? ??? ?//①.第一種方式:開啟另一個Activity
  1. ?//Intent?intent=new?Intent(this,SecondActivity.class);
  2. Intent?intent=new?Intent();
????????//②.第二種方式:使用ComponentName ????????//使用ComponentName組件名來開啟另一個Activity
  1. ComponentName?component=new?ComponentName(this,?SecondActivity.class);
????????//第二個參數(shù)是包含包名,類名的一個完整字符串
  1. ComponentName?component=new?ComponentName(this,?"com.qianfeng.day06_intentattribute01.SecondActivity");
????????//第一個參數(shù)是單純的包名;第二個參數(shù)是包含包名,類名的一個完整字符串
  1. ComponentName?component=new?ComponentName("com.qianfeng.day06_intentattribute01",?"com.qianfeng.day06_intentattribute01.SecondActivity");
  2. intent.setComponent(component);
????????//③.第三種方式:setClass也可以實現(xiàn)跳轉(zhuǎn)
  1. intent.setClass(this,?SecondActivity.class);
????????//④.第四種方式:setClassName跳轉(zhuǎn)方式
  1. intent.setClassName(this,?"com.qianfeng.day06_intentattribute01.SecondActivity");?
????????//⑤.第五種:通過隱式意圖開啟另一個Activity
  1. intent.setAction("ergouzisimida");
????????//其實系統(tǒng)默認(rèn)就有這句話,默認(rèn)省略了這句話
  1. intent.addCategory("android.intent.category.DEFAULT");
  1. startActivity(intent);
二、Intent對象的七個屬性 <1>ComponentName屬性:
? ??? ??1、指定了ComponentName屬性的Intent已經(jīng)明確了它將要啟動哪個組件,因此這種Intent被稱為顯式Intent,沒有指定ComponentName屬性的Intent被稱為隱式Intent。隱式Intent沒有明確要啟動哪個組件,應(yīng)用會根據(jù)Intent指定的規(guī)則去啟動符合條件的組件。 ? ??? ??2、示例代碼:
  1. Intent?intent?=?new?Intent();
  2. ComponentName?cName?=?new?ComponentName(MainActivity.this,NextActivity.class);
  3. intent.setComponent(cName);
  4. startActivity(intent);
  5. //實際上,以上的寫法都被簡化為以下寫法:
  6. Intent?intent?=?new?Intent(MainActivity.this,NextActivity.class);
  7. ????????startActivity(intent);
  8. //也就是說,平時我們最常用的Intent頁面跳轉(zhuǎn)的寫法就調(diào)用的是顯式Intent。
<2>Action屬性 Action、Category屬性與intent-filter配置: ? ?? ???通常,Action、Category屬性結(jié)合使用,定義這兩個屬性都是在配置文件的<intent-filter>節(jié)點(diǎn)中。Intent通過定義Action屬性(其實就是一段自定義的字符串),這樣就可以把Intent與具體的某個Activity分離,實現(xiàn)了解耦。否則,每次跳轉(zhuǎn),都要寫成類似new?Intent(MainActivity.this,NextActivity.class)這樣的形式,也就是說必須將要跳轉(zhuǎn)的目標(biāo)Activity的名字寫出來,這樣的編碼其實是“硬編碼”,并沒有實現(xiàn)松耦合。調(diào)用Intent對象的setAction()方法實現(xiàn)頁面跳轉(zhuǎn)雖然略微復(fù)雜(需要在AndroidManifest.xml文件中配置),但是實現(xiàn)了解耦。 1、示例代碼:
  1. Intent?intent?=?new?Intent();?
  2. intent.setAction("com.train.task01.editactivity");?
  3. startActivity(intent);
//在配置文件中注冊Activity的時候需要聲明:
  1. <activity?android:name="com.train.taskstack01.EditActivity">
  2. ??<intent-filter>
  3. ??????<action?android:name="com.train.task01.editactivity"?/>
  4. ??????<category?android:name="android.intent.category.DEFAULT"?/>???
  5. ??</intent-filter>
  6. </activity>
//當(dāng)某個頁面是默認(rèn)啟動頁面時,需要定義Action、Category的屬性必須為以下字符串:【設(shè)置任務(wù)入口】
  1. ????????????<intent-filter>
  2. ????????????????<action?android:name="android.intent.action.MAIN"?/>
  3. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>
  4. ????????????</intent-filter>
2、常用的屬性 ACTION_MAIN:(android.intent.action.MAIN)Android程序入口。? ACTION_VIEW:?(android.intent.action.VIEW)?顯示指定數(shù)據(jù)。
ACTION_EDIT:?(android.intent.action.EDIT)?編輯指定數(shù)據(jù)。? ACTION_DIAL:?(android.intent.action.DIAL)?顯示撥號面板。
ACTION_CALL:?(android.intent.action.CALL)?直接呼叫Data中所帶的號碼。??
ACTION_ANSWER:?(android.intent.action.ANSWER)?接聽來電。??
ACTION_SEND:?(android.intent.action.SEND)?向其他人發(fā)送數(shù)據(jù)(例如:彩信/email)。??
ACTION_SENDTO:??(android.intent.action.SENDTO)?向其他人發(fā)送短信。 ACTION_SEARCH:?(android.intent.action.SEARCH)?執(zhí)行搜索。??
ACTION_GET_CONTENT:?(android.intent.action.GET_CONTENT)?讓用戶選擇數(shù)據(jù),并返回所選數(shù)據(jù)。 <3>? Category屬性 Category屬性為Action增加額外的附加類別信息。CATEGORY_LAUNCHER意味著在加載程序的時候Acticity出現(xiàn)在最上面,而CATEGORY_HOME表示頁面跳轉(zhuǎn)到HOME界面。 實現(xiàn)頁面跳轉(zhuǎn)到HOME界面的代碼:【記憶】
  1. Intent?intent?=?new?Intent();?
  2. intent.setAction(Intent.ACTION_MAIN);?
  3. intent.addCategory(Intent.CATEGORY_HOME);?
  4. startActivity(intent);?
1、常用屬性 CATEGORY_DEFAULT:?(android.intent.category.DEFAULT)?Android系統(tǒng)中默認(rèn)的執(zhí)行方式,按照普通Activity的執(zhí)行方式執(zhí)行。??
CATEGORY_DEFAULT:?(android.intent.category.DEFAULT)?Android系統(tǒng)中默認(rèn)的執(zhí)行方式,按照普通Activity的執(zhí)行方式執(zhí)行。? CATEGORY_HOME:?(android.intent.category.HOME)?設(shè)置該組件為Home?Activity。
CATEGORY_PREFERENCE:?(android.intent.category.PREFERENCE)?設(shè)置該組件為Preference。? CATEGORY_LAUNCHER:?(android.intent.category.LAUNCHER)?設(shè)置該組件為在當(dāng)前應(yīng)用程序啟動器中優(yōu)先級最高的Activity,通常與入口ACTION_MAIN配合使用。CATEGORY_BROWSABLE:?(android.intent.category.BROWSABLE)?設(shè)置該組件可以使用瀏覽器啟動。 <4>Data屬性
Data屬性通常用于向Action屬性提供操作的數(shù)據(jù)。Data屬性的值是個Uri對象。 ? ? ?Uri的格式如下:scheme://host:port/path Intent利用Action屬性和Data屬性啟動Android系統(tǒng)內(nèi)置組件代碼: //發(fā)送短信
  1. public?void?sendSMS(View?view){
  2. ???Intent?intent=new?Intent(Intent.ACTION_SENDTO);
  3. ???intent.setData(Uri.parse("smsto:13366201398"));
  4. ???intent.putExtra("sms_body",?"發(fā)送短信的內(nèi)容");
  5. ???startActivity(intent);
  6. }
//打電話, ?<!--?增加打電話的權(quán)限?-->
  1. <uses-permission?android:name="android.permission.CALL_PHONE"?/>
  2. public?void??callPhone(View?view){
  3. ???Intent?intent=new?Intent(Intent.ACTION_CALL,Uri.parse("tel:13366201398"));
  4. ???startActivity(intent);
  5. }
//打開網(wǎng)頁 <!--?增加訪問網(wǎng)絡(luò)的權(quán)限?-->
  1. <uses-permission?android:name="android.permission.INTERNET"?/>
  1. public?void?openHtml(View?view){
  2. ???Intent?intent=new?Intent(Intent.ACTION_VIEW,Uri.parse("https://www.baidu.com/"));
  3. ???startActivity(intent);
  4. }
//打開網(wǎng)絡(luò)圖片
  1. public?void?openImage(View?view){
  2. ???Intent?intent=new?Intent(Intent.ACTION_VIEW,Uri.parse("http://192.168.129.94:8080/images/s1.jpg"));
  3. ???startActivity(intent);
  4. }
//打開相冊
  1. public?void?openAlbum(View?view){
  2. ???Intent?intent?=?new?Intent();?
  3. ???intent.setAction(Intent.?ACTION_GET_CONTENT?);
  4. ???intent.setType(?"image/*"?);?
  5. ???startActivityForResult(wrapperIntent,?100);??
  6. }
//打開相機(jī)
  1. Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);?
  2. startActivityForResult(intent,?100);??
1、常用屬性 tel://:號碼數(shù)據(jù)格式,后跟電話號碼。? mailto://:郵件數(shù)據(jù)格式,后跟郵件收件人地址。
smsto://:短息數(shù)據(jù)格式,后跟短信接收號碼。??
content://:內(nèi)容數(shù)據(jù)格式,后跟需要讀取的內(nèi)容。??
file://:文件數(shù)據(jù)格式,后跟文件路徑。??
market://search?q=pname:pkgname:市場數(shù)據(jù)格式,在Google?Market里搜索包名為pkgname的應(yīng)用。??
geo://latitude,?longitude:經(jīng)緯數(shù)據(jù)格式,在地圖上顯示經(jīng)緯度所指定的位置。 <5>Type屬性 1、Type屬性用于指定Data所指定的Uri對應(yīng)的MIME類型。MIME只要符合“abc/xyz”這樣的字符串格式即可。 2、?Intent利用Action、Data和Type屬性啟動Android系統(tǒng)內(nèi)置組件的代碼: 播放視頻:
  1. Intent?intent?=?new?Intent();?
  2. Uri?uri?=?Uri.parse("file:///sdcard/media.mp4");?
  3. intent.setAction(Intent.ACTION_VIEW);
  4. intent.setDataAndType(uri,?"video/*");?
  5. startActivity(intent);
<6>Flag屬性
Intent可調(diào)用addFlags()方法來為Intent添加控制標(biāo)記 實例:
  1. Intent?intent?=?new?Intent(this,?MainActivity.class);?
  2. //將Activity棧中處于MainActivity主頁面之上的Activity都彈出。?
  3. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);?
  4. startActivity(intent);
1、常用屬性 FLAG_ACTIVITY_CLEAR_TOP:(效果同Activity??LaunchMode的singleTask?) FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity?LaunchMode的singleTop) <7>Extra屬性 1、通過intent.putExtra(鍵,?值)的形式在多個Activity之間進(jìn)行數(shù)據(jù)交換。? 2、系統(tǒng)內(nèi)置的幾個Extra常量: ??EXTRA_BCC:存放郵件密送人地址的字符串?dāng)?shù)組。 ??EXTRA_CC:存放郵件抄送人地址的字符串?dāng)?shù)組。 ??EXTRA_EMAIL:存放郵件地址的字符串?dāng)?shù)組。 ??EXTRA_SUBJECT:存放郵件主題字符串。 ??EXTRA_TEXT:存放郵件內(nèi)容。 ??EXTRA_KEY_EVENT:以KeyEvent對象方式存放觸發(fā)Intent的按鍵。 ??EXTRA_PHONE_NUMBER:存放調(diào)用ACTION_CALL時的電話號碼。 3、?Intent利用Action、Data和Type、Extra屬性啟動Android系統(tǒng)內(nèi)置組件的代碼: //調(diào)用發(fā)送短信的程序?
  1. Intent??intent??=?new?Intent();
  2. intent.setAction(Intent.ACTION_VIEW);
  3. intent.setType("vnd.android-dir/mms-sms");?
  4. intent.putExtra("sms_body",?"信息內(nèi)容...");?
  5. startActivity(intent);
//發(fā)送短信息?
  1. Uri?uri?=?Uri.parse("smsto:13200100001");?
  2. Intent??intent??=?new?Intent();?
  3. intent.setAction(Intent.??ACTION_SENDTO?);
  4. intent.setData(uri);
  5. intent.putExtra("sms_body",?"信息內(nèi)容...");?
  6. startActivity(?intent?);
//發(fā)送彩信,設(shè)備會提示選擇合適的程序發(fā)送?
  1. Uri?uri?=?Uri.parse("content://media/external/images/media/23");?//設(shè)備中的資源(圖像或其他資源)?
  2. Intent?intent?=?new?Intent();?
  3. intent.setAction(Intent.??ACTION_SEND?);
  4. intent.setType("image/png");?
  5. intent.putExtra("sms_body",?"內(nèi)容");?
  6. intent.putExtra(Intent.EXTRA_STREAM,?uri);?
  7. startActivity(it);
//發(fā)送Email:
  1. Intent?intent=new?Intent();?
  2. intent.setAction(Intent.??ACTION_SEND?);
  3. String[]?tos={"android1@163.com"};?
  4. String[]?ccs={"you@yahoo.com"};?
  5. intent.putExtra(Intent.EXTRA_EMAIL,?tos);?
  6. intent.putExtra(Intent.EXTRA_CC,?ccs);
  7. intent.putExtra(Intent.EXTRA_TEXT,?"The?email?body?text");?
  8. intent.putExtra(Intent.EXTRA_SUBJECT,?"The?email?subject?text");?
  9. intent.setType("message/rfc822");?
  10. startActivity(Intent.createChooser(intent,?"Choose?Email?Client"));
4、?Intent利用Action屬性中的ACTION_GET_CONTENT獲取返回值: //選擇圖片?requestCode?返回的標(biāo)識
    1. Intent?intent?=?new?Intent();?
    2. intent.setAction(Intent.?ACTION_GET_CONTENT?);
    3. intent.setType(?"image/*"?);?
    4. Intent?wrapperIntent?=?Intent.createChooser(intent,?null);
    5. startActivityForResult(wrapperIntent,?requestCode); ?

轉(zhuǎn)載于:https://www.cnblogs.com/android-blogs/p/5690844.html

總結(jié)

以上是生活随笔為你收集整理的Intent七大属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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