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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android两个注意事项.深入了解Intent和IntentFilter(两)

發(fā)布時間:2023/12/9 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android两个注意事项.深入了解Intent和IntentFilter(两) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
深入理解Intent和IntentFiler(二) 轉(zhuǎn)載請表明出處:http://blog.csdn.net/u012637501(嵌入式_小J的天空) ? ??在上一篇文章中,我們比較具體學(xué)習(xí)了"Intent"的相關(guān)知識,如今將學(xué)習(xí)怎樣設(shè)置Intent對象的這些屬性以及怎樣使用他們來啟動組件。

Intent對象是一組信息,我們能夠通過設(shè)置其Action、Data、Category屬性來指定啟動哪個組件并完畢什么樣的動作(包括動作所需的數(shù)據(jù))。

? ? "意圖"分為顯示intent和隱式intent。

所謂顯示intent,指的是Intent已經(jīng)明白了它將要啟動哪個組件-通過指定Intent對象的Component屬性實現(xiàn)。而隱式intent,

指的是Intent不能確定它將要啟動哪個組件(沒有指定Component屬性)-通過AndroidManifest.xml文件里的Intent Filter來對組件進(jìn)行篩選來確定啟動的組件。 一、顯"intent" ? ??"意圖"。顧名思義,就是指Intent已經(jīng)明白以我們可了它將要啟動哪個組件。因為一個組件類能夠通過其所在的包名和類名唯一的確定,所以通過intent的Component屬性來實現(xiàn)。Intent的Component屬性須要接受一個ComponentName對象。其構(gòu)造函數(shù)傳入了該組件的包名、類名。

1."intent"開發(fā)基本思路 (1)創(chuàng)建一個ComponentName對象,用于為"意圖"指定組件包名、類名 ComponentName comp=new ComponentName(ComponentAttr.this,SecondaryActivity.class); (2)創(chuàng)建一個Intent對象,并為該對象設(shè)置Component屬性 Intent intent=new Intent(); intent.setComponent(comp); 注意:通過使用Intent不同的構(gòu)造函數(shù),(1)、(2)步驟能夠合并為: ? ? Intent intent=new Intent(ComponentAttr.this,SecondaryActivity.class); (3)啟動一個類名SecondaryActivity的Activity組件 startActivity(intent); 或者 startActivityForResult(intent,requestCode); //關(guān)閉啟動的Activity會返回結(jié)果 (4)實現(xiàn)被啟動的組件SecondaryActivity類(繼承于Activity) (5)在AndroidManifest.xml加入一個<Activity></Activity>元素,可是無需配置<intent-filter>元素 <!-- 被intent啟動的activity --><activityandroid:name=".SecondaryActivity"android:label="第二個Activity界面" ><intent-filter><action android:name="action.CRAZYIT_ACTION"/><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
博主筆記1:除了上述Intent的setComponent方法,我們還能夠利用setClass來指定須要啟動的詳細(xì)組件 Intent intent=new Intent(); intent.setClass(ComponentAttr.this,SecondaryActivity.class); startActivity(intent);

二、隱式intent ? ? 隱式intent,顧名思義,就是沒有指明intent究竟要啟動哪個組件。

顯式intent能夠通過設(shè)置其Component屬性實現(xiàn)。而隱式intent就通過Intent Filter來實現(xiàn)

詳細(xì)的說就是,我們事先設(shè)置好"意圖"啟動組件的相關(guān)信息(intent屬性),然后再在其它組件的AndroidManifest.xml文件設(shè)置好對應(yīng)的intent屬性。當(dāng)組件發(fā)出"意圖"時。Android系統(tǒng)通過查找project文件

AndroidManifest.xml(或者系統(tǒng)級組件)其它組件的<intent-filter/>相關(guān)信息來進(jìn)行匹配。篩選得到滿足"意圖"條件的組件。 1.使用Action屬性開發(fā)基本思路 ? ? ? ?Intent是組件之間的通信的載體,組件的通信能夠分為應(yīng)用內(nèi)部組件之間的通信和應(yīng)用間的通信。Intent的Action、Category屬性都是一個普通的字符串,當(dāng)中Action代表Intent索要完畢的一個"抽象"動作,Category屬性用于為Action添加附加的類別信息。 (1)應(yīng)用內(nèi)部組件通信-自己定義字符串 方式 public final String CUSTOME_ACTION="intent.action.CUSTOME_JIANG";//字符串能夠隨意 Intent intent=new Intent(); //創(chuàng)建一個Intent對象 intent.setAction(ActionAttr.CUSTOME_ACTION); //注意:ActionAttr為我們創(chuàng)建的類 startActivity(intent); //啟動一個Activity (2)與其它應(yīng)用程序通信-使用系統(tǒng)預(yù)定action常量 Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); //當(dāng)中ACTION_CALL為Intent類的靜態(tài)成員變量,能夠類直接調(diào)用 startActivity(intent); 2.使用Action、Category屬性開發(fā)基本思路 (1)應(yīng)用內(nèi)部組件通信-自己定義字符串 方式 public final String CUSTOME_ACTION="intent.action.CUSTOME_JIANG";//字符串能夠隨意 public final String CUSTOME_CATEGORY="intent.action.CUSTOME_CATEGORY";//字符串能夠隨意 Intent intent=new Intent(); //創(chuàng)建一個Intent對象 intent.setAction(ActionAttr.CUSTOME_ACTION); //注意:ActionAttr為我們創(chuàng)建的類 intent.addCategory(ActionAttr.CUSTOME_CATEGORY); startActivity(intent); //啟動一個Activity (2)使用系統(tǒng)預(yù)定action、category常量-下面代碼實現(xiàn)當(dāng)點擊某個button時,通過Intent對象實現(xiàn)返回HOME桌面。

Intent intent=new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME);//返回Home桌面 startActivity(intent); //啟動一個Activity 注意:這里無需設(shè)置AndroidManifest.xml 假設(shè)是實現(xiàn)應(yīng)用內(nèi)部組件的通信。除了上述(1)(2)步驟,我們還須要完畢步: (3)實現(xiàn)須要啟動的Activity,如SecondaryActivity.java,ThirdActivity.java,使其繼承于Activity; (4)在projectAndroidManifest.xml加入<activity></activity>元素,并加入<intent-filter/>相關(guān)信息 <activity android:name=".SecondaryActivity" android:label="第二個Activity界面" > <intent-filter> <action android:name="intent.action.JIANG_ACTION" /> <category android:name="intent.action.JIANG_CATEGORY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".ThirdActivity" android:label="第三個Activity界面" > <intent-filter> <action android:name="intent.action.JIANG_ACTION" /> <category android:name="intent.action.JIANG_CATEGORY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
博主筆記2:實際上。我們在開發(fā)包括"意圖"的應(yīng)用程序中,Action屬性和Category屬性是配合使用的。由于,Android系統(tǒng)會給主動Activity在AndroidManifest.xml中默認(rèn)一個Action屬性和Category屬性。即: ?<intent-filter> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" /> ? ? ? ? ? ? ? ? ? ? ? ?//應(yīng)用程序入口 ? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" /> ? </intent-filter> 另外,還須要注意幾點: 1.Action常量等屬性。如ACTION_CALL是在設(shè)置intent時使用,其相應(yīng)的字符串"android.intent.action.CALL"在AndroidManifest.xml"中使用; 2.當(dāng)使用Action屬性等的系統(tǒng)提前定義常量,與其它應(yīng)用通信時,僅僅須要在本應(yīng)用的AndroidManifest.xml加入對應(yīng)的權(quán)限就可以。

3.一個Activity中僅僅能且必須定義一個Action屬性和一個Category屬性,當(dāng)中。Category屬性系統(tǒng)會分配其默認(rèn)常量"CATEGORY_DEFAULT"
3.源碼 ? ? 該實例主要完畢2個功能: (1)實現(xiàn)一個button。使用Action屬性和Category屬性啟動一個Activity; (2)實現(xiàn)一個button,用于返回HOME界面 (1).FirstActivity.java:主Activity package com.example.android_intent_2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ActionCateAttr extends Activity {//自己定義一個action常量org.crazyit.public final static String CRAZYIT_ACTION="intent.action.JIANG_ACTION";public final static String CRAZYIT_CATEGORY="intent.action.JIANG_CATEGORY";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first);Button btn=(Button)findViewById(R.id.button);btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//創(chuàng)建一個Intent對象Intent intent=new Intent();intent.setAction(ActionCateAttr.CRAZYIT_ACTION); //設(shè)置action屬性intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY); //設(shè)置category屬性startActivity(intent);}});//為btn注冊一個事件監(jiān)聽器對象/*返回桌面按鈕*/Button btn1=(Button)findViewById(R.id.home);btn1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//創(chuàng)建一個Intent對象Intent intent=new Intent();intent.setAction(Intent.ACTION_MAIN); //設(shè)置action屬性intent.addCategory(Intent.CATEGORY_HOME);//設(shè)置category屬性startActivity(intent);}});//為btn注冊一個事件監(jiān)聽器對象} } (2).AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.android_intent_2"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="11"android:targetSdkVersion="14" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".ActionCateAttr"android:label="第一個Activity界面" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".SecondaryActivity"android:label="第二個Activity界面" ><intent-filter><action android:name="intent.action.JIANG_ACTION" /><category android:name="intent.action.JIANG_CATEGORY" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity><activityandroid:name=".ThirdActivity"android:label="第三個Activity界面" ><intent-filter><action android:name="intent.action.JIANG_ACTION" /><category android:name="intent.action.JIANG_CATEGORY" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application> </manifest> (3)在project中,加入SecondaryActivity.java,ThirdActivity.java繼承于Activity.
(4)效果
3.使用Data、Type屬性開發(fā)基本思路 ? ? Action屬性為Intent對象描寫敘述了一個"動作"。那么Data屬性就為Intent對象的Action屬性提供了操作的數(shù)據(jù)。Type屬性用于指定該Data所指定Uri相應(yīng)的MIME類型,這樣的類型能夠是不論什么自己定義的MIME類型,僅僅要符合abc/xyz格式的字符串就可以。

這里須要注意的是,Type屬性和Data屬性通常會出現(xiàn)相互覆蓋的情況,假設(shè)希望Intent既有Data屬性也有Type屬性,必須通過setDataAndType()方法來實現(xiàn)。

這里須要注意的是,Data屬性僅僅接受一個Uri對象。一個Uri對象通常通過例如以下形式的字符串來表示:

? ??Uri字符串格式:scheme://host:port/path 舉例: content://com.android.contacts/contacts/1或tel://18819463209 ? ? 這里有兩種情況。一是啟動系統(tǒng)級應(yīng)用程序;二是。啟動應(yīng)用內(nèi)部組件。前者無需配置AndroidManifest.xml中的<data../>元素。僅僅需加入對應(yīng)權(quán)限就可以;后者。須要配置AndroidManifest.xml中的<data/>元素內(nèi)容。當(dāng)中。為組件聲明Data、Type屬性都通過<data../>元素,格式例如以下: ? ? <data android:mimeType="" ? ? ? ?//用于聲明該組件所能匹配的Intent的Type屬性 ? ? ? ? ? ? android:scheme="" ? ? ? ? ? ? //協(xié)議 ? ? ? ? ? ? ?android:host="" ? ? ? ? ? ? ? ? //用于聲明該組件所能匹配的Intent的Data屬性host部分(主機) ? ? ? ? ? ? ?android:port="" ? ? ? ? ? ? ? ? //串口 ? ? ? ? ? ? ?android:path="" ? ? ? ? ? ? ? ?//資源路徑 ? ? ? ? ? ? android:pathPrefix="" ? ? ? ?//Data屬性的前綴 ? ? ? ? ? ? android:pathPattern=""/> ? ?//Data屬性的path字符串模板 情況一、啟動系統(tǒng)級應(yīng)用組件 (1)實現(xiàn)一個Intent對象。并啟動組件 Intent intent=new Intent(); //創(chuàng)建一個Intent對象 String data="content://com.android.contacts/contacts/1"; Uri uri=Uri.parse(data); //將字符串轉(zhuǎn)換為Uri intent.setAction(Intent.ACTION_VIEW); //設(shè)置Intent對象Action屬性 intent.setData(uri); //設(shè)置Intent對象Data屬性 startActivity(intent); 或者 Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("content://com.android.contacts/contacts/1")); startActivity(intent); (2)在AndroidManifest.xml中加入對應(yīng)的權(quán)限 在Android中讀取電話信息時,要注意增加 <use-permission android:name="android.permission.READ_CONTACTS"/> 在android中使用BroadcastReceiver時 <use-permission android:name="android.permission.RECEIVE_SMS"/> 在android中使用有關(guān)的文件下載功能時,要使用到的 <use-permission android:name="android.permission.INTERNET"/> <use-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> (3)源碼 ? ? 該應(yīng)用程序?qū)崿F(xiàn)三個button實現(xiàn)三個功能:打開網(wǎng)頁、編輯聯(lián)系人、撥打電話 firstActivity.java package com.android.android_intent_4; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.content.Intent; import android.net.Uri; import android.os.Bundle; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*第一個按鍵功能:打開網(wǎng)頁*/ Button btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener(){ //為按鈕1注冊一個事件監(jiān)聽器對象 @Override public void onClick(View v) { //1.創(chuàng)建Intent Intent intent=new Intent(); //2.設(shè)置action、data屬性 String data="http://www.baidu.com"; Uri uri=Uri.parse(data); //將字符串轉(zhuǎn)化為Uri-通用資源標(biāo)識 intent.setAction(Intent.ACTION_VIEW); //設(shè)置intent屬性為系統(tǒng)提前定義的Intent.ACTION_VIEW intent.setData(uri); //為intent設(shè)置數(shù)據(jù)屬性。用于傳遞數(shù)據(jù) //3.啟動Activity startActivity(intent); }}); /*第二個按鍵功能:編輯標(biāo)識為1的聯(lián)系人*/ Button btn2=(Button)findViewById(R.id.button2); btn2.setOnClickListener(new OnClickListener(){ //為按鈕1注冊一個事件監(jiān)聽器對象 @Override public void onClick(View v) { //1.創(chuàng)建Intent Intent intent=new Intent(); //2.設(shè)置action、data屬性 intent.setAction(Intent.ACTION_EDIT); //設(shè)置intent屬性為系統(tǒng)提前定義的Intent.ACTION_VIEW intent.setData(Uri.parse("content://com.android.contacts/contacts/1")); //為intent設(shè)置數(shù)據(jù)屬性。依據(jù)指定的字符解析出Uri對象 //3.啟動Activity startActivity(intent); }}); /*第三個按鍵功能:撥打電話18819465188*/ Button btn3=(Button)findViewById(R.id.button3); btn3.setOnClickListener(new OnClickListener(){ //為按鈕1注冊一個事件監(jiān)聽器對象 @Override public void onClick(View v) { //1.創(chuàng)建Intent Intent intent=new Intent(); //2.設(shè)置action、data屬性 intent.setAction(Intent.ACTION_DIAL); //設(shè)置intent屬性為系統(tǒng)提前定義的Intent.ACTION_VIEW intent.setData(Uri.parse("tel:18819465188")); //依據(jù)指定的字符解析出Uri對象 //3.啟動Activity startActivity(intent); }}); } } 效果例如以下圖:
情況二、啟動應(yīng)用內(nèi)部組件 (1)實現(xiàn)一個Intent對象,并啟動組件 Intent intent=new Intent(); //創(chuàng)建一個Intent對象 String data="lee://www.fkjava.org:8888/mypath"; Uri uri=Uri.parse(data); //將字符串轉(zhuǎn)換為Uri intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); //設(shè)置Intent對象Data屬性 startActivity(intent); 或者 Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath")); startActivity(intent); (2)AndroidManifest.xml中設(shè)置<data../>元素中的相關(guān)內(nèi)容就可以 <data android:mimeType=""android:host=""android:port=""android:path=""android:pathPrefix=""android:pathPattern=""/> (3)實現(xiàn)其他Activity

版權(quán)聲明:本文博主原創(chuàng)文章。博客,未經(jīng)同意不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的Android两个注意事项.深入了解Intent和IntentFilter(两)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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