小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

第十章:Intent詳解

 kiki的號 2017-04-14

【正文】


Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個知識,也非常的重要。


一、什么是Intent


1、Intent的概念:



  • Android中提供了Intent機制來協(xié)助應(yīng)用間的交互與通訊,或者采用更準(zhǔn)確的說法是,Intent不僅可用于應(yīng)用程序之間,也可用于應(yīng)用程序內(nèi)部的activity, service和broadcast receiver之間的交互。Intent這個英語單詞的本意是“目的、意向、意圖”。

  • Intent是一種運行時綁定(runtime binding)機制,它能在程序運行的過程中連接兩個不同的組件。通過Intent,你的程序可以向Android表達(dá)某種請求或者意愿,Android會根據(jù)意愿的內(nèi)容選擇適當(dāng)?shù)慕M件來響應(yīng)。


activity、service和broadcast receiver之間是通過Intent進行通信的,而另外一個組件Content Provider本身就是一種通信機制,不需要通過Intent。我們來看下面這個圖就知道了:



如果Activity1需要和Activity2進行聯(lián)系,二者不需要直接聯(lián)系,而是通過Intent作為橋梁。通俗來講,Intnet類似于中介、媒婆的角色。


 


2、對于向這三種組件發(fā)送intent有不同的機制:



  • 使用Context.startActivity() 或 Activity.startActivityForResult(),傳入一個intent來啟動一個activity。使用 Activity.setResult(),傳入一個intent來從activity中返回結(jié)果。



  • 將intent對象傳給Context.startService()來啟動一個service或者傳消息給一個運行的service。將intent對象傳給 Context.bindService()來綁定一個service。



  • 將intent對象傳給 Context.sendBroadcast(),Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等廣播方法,則它們被傳給 broadcast receiver。



二、Intent的相關(guān)屬性:



  • Intent由以下各個組成部分:

  • component(組件):目的組件

  • action(動作):用來表現(xiàn)意圖的行動

  • category(類別):用來表現(xiàn)動作的類別

  • data(數(shù)據(jù)):表示與動作要操縱的數(shù)據(jù)

  • type(數(shù)據(jù)類型):對于data范例的描寫

  • extras(擴展信息):擴展信息

  • Flags(標(biāo)志位):期望這個意圖的運行模式


Intent類型分為顯式Intent(直接類型)、隱式Intent(間接類型)。官方建議使用隱式Intent。上述屬性中,component屬性為直接類型,其他均為間接類型。


相比與顯式Intent,隱式Intnet則含蓄了許多,它并不明確指出我們想要啟動哪一個活動,而是指定一系列更為抽象的action和category等信息,然后交由系統(tǒng)去分析這個Intent,并幫我們找出合適的活動去啟動。


Activity 中 Intent Filter 的匹配過程 :



 


1、component(組件):目的組件


Component屬性明確指定Intent的目標(biāo)組件的類名稱。(屬于直接Intent)


如果 component這個屬性有指定的話,將直接使用它指定的組件。指定了這個屬性以后,Intent的其它所有屬性都是可選的。


例如,啟動第二個Activity時,我們可以這樣來寫:


復(fù)制代碼

復(fù)制代碼

 1         button1.setOnClickListener(new OnClickListener() {            
 2             @Override
 3             public void onClick(View v) {
 4                 //創(chuàng)建一個意圖對象
 5                 Intent intent = new Intent();
 6                 //創(chuàng)建組件,通過組件來響應(yīng)
 7                 ComponentName component = new ComponentName(MainActivity.this, SecondActivity.class);
 8                 intent.setComponent(component);                
 9                 startActivity(intent);                
10             }
11         });

復(fù)制代碼

復(fù)制代碼

如果寫的簡單一點,監(jiān)聽事件onClick()方法里可以這樣寫:



1                 Intent intent = new Intent();
2                 //setClass函數(shù)的第一個參數(shù)是一個Context對象
3                 //Context是一個類,Activity是Context類的子類,也就是說,所有的Activity對象,都可以向上轉(zhuǎn)型為Context對象
4                 //setClass函數(shù)的第二個參數(shù)是一個Class對象,在當(dāng)前場景下,應(yīng)該傳入需要被啟動的Activity類的class對象
5                 intent.setClass(MainActivity.this, SecondActivity.class);
6                 startActivity(intent);    


再簡單一點,可以這樣寫:(當(dāng)然,也是最常見的寫法)



1                 Intent intent = new Intent(MainActivity.this,SecondActivity.class);
2                 startActivity(intent);


 


 


2、Action(動作):用來表現(xiàn)意圖的行動


當(dāng)日常生活中,描述一個意愿或愿望的時候,總是有一個動詞在其中。比如:我想“做”三個俯臥撐;我要“寫” 一封情書,等等。在Intent中,Action就是描述做、寫等動作的,當(dāng)你指明了一個Action,執(zhí)行者就會依照這個動作的指示,接受相關(guān)輸入,表現(xiàn)對應(yīng)行為,產(chǎn)生符合的輸出。在Intent類中,定義了一批量的動作,比如ACTION_VIEW,ACTION_PICK等, 基本涵蓋了常用動作。加的動作越多,越精確。


Action 是一個用戶定義的字符串,用于描述一個 Android 應(yīng)用程序組件,一個 Intent Filter 可以包含多個 Action。在 AndroidManifest.xml 的Activity 定義時,可以在其 <intent-filter >節(jié)點指定一個 Action列表用于標(biāo)識 Activity 所能接受的“動作”。


 


3、category(類別):用來表現(xiàn)動作的類別


Category屬性也是作為<intent-filter>子元素來聲明的。例如:


<intent-filter>


  <action android:name="com.vince.intent.MY_ACTION"></action>


  <category android:name="com.vince.intent.MY_CATEGORY"></category> 


  <category android:name="android.intent.category.DEFAULT"></category> 


</intent-filter>   


Action 和category通常是放在一起用的,所以這里一起介紹一下。我們來先來舉一個例子:


新建一個工程文件smyh006_Intent01,在默認(rèn)文件的基礎(chǔ)之上,新建文件SecondActicity.java和activity_second.xml。


緊接著,我們要到清單文件中進行注冊,打開AndroidManifest.xml,添加SecondActivity的action和category的過濾器:


復(fù)制代碼

復(fù)制代碼

1         <activity 
2             android:name=".SecondActivity">
3             <intent-filter>
4                  <action android:name="com.example.smyh006intent01.MY_ACTION"/>
5                  <category android:name="android.intent.category.DEFAULT" />
6             </intent-filter>            
7         </activity>

復(fù)制代碼

復(fù)制代碼

上方代碼,表示SecondActicity可以匹配第4行的MY_ACTION這個動作,此時,如果在其他的Acticity通過這個action的條件來查找,那SecondActicity就具備了這個條件。類似于相親時,我要求對方有哪些條件,然后對方這個SecondActicity恰巧滿足了這個條件(夠通俗了吧)。


注:如果沒有指定的category,則必須使用默認(rèn)的DEFAULT(即上方第5行代碼)。


也就是說:只有<action>和<category>中的內(nèi)容同時能夠匹配上Intent中指定的action和category時,這個活動才能響應(yīng)Intent。如果使用的是DEFAULT這種默認(rèn)的category,在稍后調(diào)用startActivity()方法的時候會自動將這個category添加到Intent中。


現(xiàn)在來修改MainActivity.java中按鈕的點擊事件,代碼如下:


復(fù)制代碼

復(fù)制代碼

 1         button1.setOnClickListener(new OnClickListener() {            
 2             @Override
 3             public void onClick(View v) {
 4                 //啟動另一個Activity,(通過action屬性進行查找)
 5                 Intent intent = new Intent();
 6                 //設(shè)置動作(實際action屬性就是一個字符串標(biāo)記而已)
 7                 intent.setAction("com.example.smyh006intent01.MY_ACTION"); //方法:Intent android.content.Intent.setAction(String action)
 8                 startActivity(intent);        
 9             }
10         });

復(fù)制代碼

復(fù)制代碼

上方代碼中,也可以換成下面這種簡潔的方式:


復(fù)制代碼

復(fù)制代碼

1         button1.setOnClickListener(new OnClickListener() {            
2             @Override
3             public void onClick(View v) {
4                 //啟動另一個Activity,(通過action屬性進行查找)
5                 Intent intent = new Intent("com.example.smyh006intent01.MY_ACTION");//方法: android.content.Intent.Intent(String action)                
6                 startActivity(intent);        
7             }
8         });

復(fù)制代碼

復(fù)制代碼

上方第5行代碼:在這個Intent中,我并沒有指定具體哪一個Activity,我只是指定了一個action的常量。所以說,隱式Intent的作用就表現(xiàn)的淋漓盡致了。此時,點擊MainActicity中的按鈕,就會跳到SecondActicity中去。


上述情況只有SecondActicity匹配成功。如果有多個組件匹配成功,就會以對話框列表的方式讓用戶進行選擇。我們來詳細(xì)介紹一下:


我們新建文件ThirdActicity.java和activity_third.xml,然后在清單文件AndroidManifest.xml中添加ThirdActivity的action和category的過濾器:


復(fù)制代碼

復(fù)制代碼

1         <activity 
2             android:name=".ThirdActivity">
3             <intent-filter>
4                  <action android:name="com.example.smyh006intent01.MY_ACTION"/>
5                  <category android:name="android.intent.category.DEFAULT" />
6             </intent-filter>            
7         </activity> 

復(fù)制代碼

復(fù)制代碼

此時,運行程序,當(dāng)點擊MainActivity中的按鈕時,彈出如下界面:



相信大家看到了這個界面,應(yīng)該就一目了然了。于是我們可以做出如下總結(jié):


在自定義動作時,使用activity組件時,必須添加一個默認(rèn)的類別


具體的實現(xiàn)為:


<intent-filter>


               <action android:name="com.example.action.MY_ACTION"/>


               <category android:name="android.intent.category.DEFAULT"/>


</intent-filter>


如果有多個組件被匹配成功,就會以對話框列表的方式讓用戶進行選擇。


每個Intent中只能指定一個action,但卻能指定多個category;類別越多,動作越具體,意圖越明確(類似于相親時,給對方提了很多要求)。


目前我們的Intent中只有一個默認(rèn)的category,現(xiàn)在可以通過intent.addCategory()方法來實現(xiàn)。修改MainActivity中按鈕的點擊事件,代碼如下:


復(fù)制代碼

復(fù)制代碼

 1         button1.setOnClickListener(new OnClickListener() {            
 2             @Override
 3             public void onClick(View v) {
 4                 //啟動另一個Activity,(通過action屬性進行查找)
 5                 Intent intent = new Intent();
 6                 //設(shè)置動作(實際action屬性就是一個字符串標(biāo)記而已)
 7                 intent.setAction("com.example.smyh006intent01.MY_ACTION"); //方法:Intent android.content.Intent.setAction(String action)
 8                 intent.addCategory("com.example.smyh006intent01.MY_CATEGORY");
 9                 startActivity(intent);        
10             }
11         });

復(fù)制代碼

復(fù)制代碼

既然在Intent中增加了一個category,那么我們要在清單文件中去聲明這個category,不然程序?qū)o法運行。代碼如下:


復(fù)制代碼

復(fù)制代碼

1             android:name=".SecondActivity">
2             <intent-filter>
3                  <action android:name="com.example.smyh006intent01.MY_ACTION"/>
4                  <category android:name="android.intent.category.DEFAULT" />
5                  <category android:name="com.example.smyh006intent01.MY_CATEGORY" />
6             </intent-filter>            
7         </activity>

復(fù)制代碼

復(fù)制代碼

此時,點擊MainActicity中的按鈕,就會跳到SecondActicity中去。


總結(jié)如下:


自定義類別: 在Intent添加類別可以添加多個類別,那就要求被匹配的組件必須同時滿足這多個類別,才能匹配成功。操作Activity的時候,如果沒有類別,須加上默認(rèn)類別


 


4、data(數(shù)據(jù)):表示與動作要操縱的數(shù)據(jù)



  • Data屬性是Android要訪問的數(shù)據(jù),和action和Category聲明方式相同,也是在<intent-filter>中。

  • 多個組件匹配成功顯示優(yōu)先級高的; 相同顯示列表。


Data是用一個uri對象來表示的,uri代表數(shù)據(jù)的地址,屬于一種標(biāo)識符。通常情況下,我們使用action+data屬性的組合來描述一個意圖:做什么


使用隱式Intent,我們不僅可以啟動自己程序內(nèi)的活動,還可以啟動其他程序的活動,這使得Android多個應(yīng)用程序之間的功能共享成為了可能。比如應(yīng)用程序中需要展示一個網(wǎng)頁,沒有必要自己去實現(xiàn)一個瀏覽器(事實上也不太可能),而是只需要條用系統(tǒng)的瀏覽器來打開這個網(wǎng)頁就行了。


【實例】打開指定網(wǎng)頁:


MainActivity.java中,監(jiān)聽器部分的核心代碼如下:


復(fù)制代碼

復(fù)制代碼

 1         button1.setOnClickListener(new OnClickListener() {            
 2             @Override
 3             public void onClick(View v) {
 4                 Intent intent = new Intent();
 5                 intent.setAction(Intent.ACTION_VIEW);
 6                 Uri data = Uri.parse("http://www.baidu.com");
 7                 intent.setData(data);                
 8                 startActivity(intent);        
 9             }
10         });

復(fù)制代碼

復(fù)制代碼

當(dāng)然,上方代碼也可以簡寫成:


復(fù)制代碼

復(fù)制代碼

1         button1.setOnClickListener(new OnClickListener() {            
2             @Override
3             public void onClick(View v) {
4                 Intent intent = new Intent(Intent.ACTION_VIEW);
5                 intent.setData(Uri.parse("http://www.baidu.com"));                
6                 startActivity(intent);        
7             }
8         });

復(fù)制代碼

復(fù)制代碼

第4行代碼:指定了Intent的action是 Intent.ACTION_VIEW,表示查看的意思,這是一個Android系統(tǒng)內(nèi)置的動作;


第5行代碼:通過Uri.parse()方法,將一個網(wǎng)址字符串解析成一個Uri對象,再調(diào)用intent的setData()方法將這個Uri對象傳遞進去。


當(dāng)點擊按鈕時,將跳到如下界面:



此時, 調(diào)用的是系統(tǒng)默認(rèn)的瀏覽器,也就是說,只調(diào)用了這一個組件?,F(xiàn)在如果有多個組件得到了匹配,應(yīng)該是什么情況呢?


我們修改修改清單文件中對SecondAcivity的聲明:


復(fù)制代碼

復(fù)制代碼

1         <activity 
2             android:name=".SecondActivity">
3             <intent-filter>
4                  <action android:name="android.intent.action.VIEW" />
5                  <category android:name="android.intent.category.DEFAULT" />
6                  <data android:scheme="http" android:host="www.baidu.com"/>                 
7             </intent-filter>            
8         </activity>

復(fù)制代碼

復(fù)制代碼

現(xiàn)在,SecondActivity也匹配成功了,我們運行程序,點擊MainActicity的按鈕時,彈出如下界面供我們選擇:



我們可以總結(jié)如下:



  • 當(dāng)Intent匹配成功的組件有多個時,顯示優(yōu)先級高的組件,如果優(yōu)先級相同,顯示列表讓用戶自己選擇

  • 優(yōu)先級從-1000至1000,并且其中一個必須為負(fù)的才有效


注:系統(tǒng)默認(rèn)的瀏覽器并沒有做出優(yōu)先級聲明,其優(yōu)先級默認(rèn)為正數(shù)。


優(yōu)先級的配置如下:


在清單文件中修改對SecondAcivity的聲明,即增加一行代碼,通過來android:priority設(shè)置優(yōu)先級,如下:


復(fù)制代碼

復(fù)制代碼

1         <activity 
2             android:name=".SecondActivity">
3             <intent-filter android:priority="-1">
4                  <action android:name="android.intent.action.VIEW" />
5                  <category android:name="android.intent.category.DEFAULT" />
6                  <data android:scheme="http" android:host="www.baidu.com"/>                                  
7             </intent-filter>            
8         </activity>

復(fù)制代碼

復(fù)制代碼

注:


Data屬性的聲明中要指定訪問數(shù)據(jù)的Uri和MIME類型??梢栽?lt;data>元素中通過一些屬性來設(shè)置:


android:scheme、android:path、android:port、android:mimeType、android:host等,通過這些屬性來對應(yīng)一個典型的Uri格式scheme://host:port/path。例如:http://www.google.com。


 


5、type(數(shù)據(jù)類型):對于data范例的描寫


如果Intent對象中既包含Uri又包含Type,那么,在<intent-filter>中也必須二者都包含才能通過測試。


Type屬性用于明確指定Data屬性的數(shù)據(jù)類型或MIME類型,但是通常來說,當(dāng)Intent不指定Data屬性時,Type屬性才會起作用,否則Android系統(tǒng)將會根據(jù)Data屬性值來分析數(shù)據(jù)的類型,所以無需指定Type屬性。


data和type屬性一般只需要一個,通過setData方法會把type屬性設(shè)置為null,相反設(shè)置setType方法會把data設(shè)置為null,如果想要兩個屬性同時設(shè)置,要使用Intent.setDataAndType()方法。


【任務(wù)】:data+type屬性的使用 【實例】:播放指定路徑的mp3文件。


具體如下:


新建工程文件smyh006_Intent02,MainActivity.java中按鈕監(jiān)聽事件部分的代碼如下:


復(fù)制代碼

復(fù)制代碼

 1         button.setOnClickListener(new OnClickListener(){
 2             @Override
 3             public void onClick(View v) {
 4                 Intent intent = new Intent();
 5                 intent.setAction(Intent.ACTION_VIEW);
 6                 Uri data = Uri.parse("file:///storage/sdcard0/平凡之路.mp3");
 7                 //設(shè)置data+type屬性
 8                 intent.setDataAndType(data, "audio/mp3"); //方法:Intent android.content.Intent.setDataAndType(Uri data, String type)
 9                 startActivity(intent);                
10             }            
11         });

復(fù)制代碼

復(fù)制代碼

代碼解釋:


第6行:"file://"表示查找文件,后面再加上我的小米手機存儲卡的路徑:/storage/sdcard0,再加上具體歌曲的路徑。


第8行:設(shè)置data+type屬性  


運行后,當(dāng)點擊按鈕時,效果如下:



上方界面中,使用的是小米系統(tǒng)默認(rèn)的音樂播放器。


 


6、extras(擴展信息):擴展信息


是其它所有附加信息的集合。使用extras可以為組件提供擴展信息,比如,如果要執(zhí)行“發(fā)送電子郵件”這個


動作,可以將電子郵件的標(biāo)題、正文等保存在extras里,傳給電子郵件發(fā)送組件。


 


7、Flags(標(biāo)志位):期望這個意圖的運行模式


一個程序啟動后系統(tǒng)會為這個程序分配一個task供其使用,另外同一個task里面可以擁有不同應(yīng)用程序的activity。那么,同一個程序能不能擁有多個task?這就涉及到加載activity的啟動模式,這個需要單獨講一下。


注:android中一組邏輯上在一起的activity被叫做task,自己認(rèn)為可以理解成一個activity堆棧。


 


三、Activity的啟動模式:(面試注意)


Activity有四種啟動模式:standard、singleTop、singleTask、singleInstance??梢栽贏ndroidManifest.xml中activity標(biāo)簽的屬性android:launchMode中設(shè)置該activity的加載模式。



  • standard模式:默認(rèn)的模式,以這種模式加載時,每當(dāng)啟動一個新的活動,必定會構(gòu)造一個新的Activity實例放到返回棧(目標(biāo)task)的棧頂,不管這個Activity是否已經(jīng)存在于返回棧中;

  • singleTop模式:如果一個以singleTop模式啟動的activity的實例已經(jīng)存在于返回桟的桟頂,那么再啟動這個Activity時,不會創(chuàng)建新的實例,而是重用位于棧頂?shù)哪莻€實例,并且會調(diào)用該實例的onNewIntent()方法將Intent對象傳遞到這個實例中;


注:如果以singleTop模式啟動的activity的一個實例已經(jīng)存在于返回桟中,但是不在桟頂,那么它的行為和standard模式相同,也會創(chuàng)建多個實例;



  • singleTask模式:這種模式下,每次啟動一個activity時,系統(tǒng)首先會在返回棧中檢查是否存在該活動的實例,如果存在,則直接使用該實例,并把這個活動之上的所有活動統(tǒng)統(tǒng)清除;如果沒有發(fā)現(xiàn)就會創(chuàng)建一個新的活動實例;



  • singleInstance模式:總是在新的任務(wù)中開啟,并且這個新的任務(wù)中有且只有這一個實例,也就是說被該實例啟動的其他activity會自動運行于另一個任務(wù)中。當(dāng)再次啟動該activity的實例時,會重新調(diào)用已存在的任務(wù)和實例。并且會調(diào)用這個實例的onNewIntent()方法,將Intent實例傳遞到該實例中。和singleTask相同,同一時刻在系統(tǒng)中只會存在一個這樣的Activity實例。(singleInstance即單實例)


注:前面三種模式中,每個應(yīng)用程序都有自己的返回棧,同一個活動在不同的返回棧中入棧時,必然是創(chuàng)建了新的實例。而使用singleInstance模式可以解決這個問題,在這種模式下會有一個單獨的返回棧來管理這個活動,不管是哪一個應(yīng)用程序來訪問這個活動,都公用同一個返回棧,也就解決了共享活動實例的問題。(此時可以實現(xiàn)任務(wù)之間的切換,而不是單獨某個棧中的實例切換)


 


其實我們不在清單文件中設(shè)置,只在代碼中通過flag來設(shè)置也是可以的,如下:



1         Intent intent = new Intent(MainActivity.this,SecondActivity.class);
2         //相當(dāng)于singleTask
3         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4         startActivity(intent);


 


 



1         Intent intent = new Intent(MainActivity.this,SecondActivity.class);
2         //相當(dāng)于singleTop
3         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
4         startActivity(intent);


 


 


三、Intent的常見應(yīng)用:


1、打開指定網(wǎng)頁:(直接復(fù)制的上面的代碼)


MainActivity.java中,監(jiān)聽器部分的核心代碼如下:


復(fù)制代碼

復(fù)制代碼

 1         button1.setOnClickListener(new OnClickListener() {            
 2             @Override
 3             public void onClick(View v) {
 4                 Intent intent = new Intent();
 5                 intent.setAction(Intent.ACTION_VIEW);//方法:android.content.Intent.Intent(String action)
 6                 Uri data = Uri.parse("http://www.baidu.com");
 7                 intent.setData(data);                
 8                 startActivity(intent);        
 9             }
10         });

復(fù)制代碼

復(fù)制代碼

當(dāng)然,上方代碼也可以簡寫成:


復(fù)制代碼

復(fù)制代碼

1   button1.setOnClickListener(new OnClickListener() {            
2             @Override
3             public void onClick(View v) {
4                 Intent intent = new Intent(Intent.ACTION_VIEW);
5                 intent.setData(Uri.parse("http://www.baidu.com"));                
6                 startActivity(intent);        
7             }
8         });

復(fù)制代碼

復(fù)制代碼

第4行代碼:指定了Intent的action是 Intent.ACTION_VIEW,表示查看的意思,這是一個Android系統(tǒng)內(nèi)置的動作;


第5行代碼:通過Uri.parse()方法,將一個網(wǎng)址字符串解析成一個Uri對象,再調(diào)用intent的setData()方法將這個Uri對象傳遞進去。


或者可以寫成:


復(fù)制代碼

復(fù)制代碼

1         button1.setOnClickListener(new OnClickListener() {            
2             @Override
3             public void onClick(View v) {
4                 Uri uri = Uri.parse("http://www.baidu.com");
5                 Intent intent = new Intent(Intent.ACTION_VIEW,uri);//方法: android.content.Intent.Intent(String action, Uri uri)        
6                 startActivity(intent);        
7             }
8         });

復(fù)制代碼

復(fù)制代碼

 


2、打電話:


【方式一】打開撥打電話的界面:


 



1                 Intent intent = new Intent(Intent.ACTION_DIAL);
2                 intent.setData(Uri.parse("tel:10086"));
3                 startActivity(intent);  


運行程序后,點擊按鈕,顯示如下界面:



 


【方式二】直接撥打電話:


 



1                 Intent intent = new Intent(Intent.ACTION_CALL);
2                 intent.setData(Uri.parse("tel:10086"));
3                 startActivity(intent);


要使用這個功能必須在配置文件中加入權(quán)限:(加一行代碼)


 



1     <uses-sdk
2         android:minSdkVersion="8"
3         android:targetSdkVersion="16" />
4     <uses-permission android:name="android.permission.CALL_PHONE"/>


 


3、發(fā)送短信:


【方式一】打開發(fā)送短信的界面:action+type


 



1         Intent intent = new Intent(Intent.ACTION_VIEW);
2         intent.setType("vnd.android-dir/mms-sms");
3         intent.putExtra("sms_body", "具體短信內(nèi)容"); //"sms_body"為固定內(nèi)容
4         startActivity(intent); 


【方式二】打開發(fā)短信的界面(同時指定電話號碼):action+data


 



1         Intent intent = new Intent(Intent.ACTION_SENDTO);
2         intent.setData(Uri.parse("smsto:18780260012"));
3         intent.putExtra("sms_body", "具體短信內(nèi)容"); //"sms_body"為固定內(nèi)容        
4         startActivity(intent);


4、播放指定路徑音樂:action+data+type


 



1         Intent intent = new Intent(Intent.ACTION_VIEW);
2         Uri uri = Uri.parse("file:///storage/sdcard0/平凡之路.mp3"); ////路徑也可以寫成:"/storage/sdcard0/平凡之路.mp3"
3         intent.setDataAndType(uri, "audio/mp3"); //方法:Intent android.content.Intent.setDataAndType(Uri data, String type)
4         startActivity(intent);


5、卸載程序:action+data(例如點擊按鈕,卸載某個應(yīng)用程序,根據(jù)包名來識別)


注:無論是安裝還是卸載,應(yīng)用程序是根據(jù)包名package來識別的。


 



1         Intent intent = new Intent(Intent.ACTION_DELETE);
2         Uri data = Uri.parse("package:com.example.smyh006intent01");
3         intent.setData(data);
4         startActivity(intent);


6、安裝程序:action+data+type


 



1         Intent intent = new Intent(Intent.ACTION_VIEW);
2         Uri data = Uri.fromFile(new File("/storage/sdcard0/AndroidTest/smyh006_Intent01.apk"));    //路徑不能寫成:"file:///storage/sdcard0/···"
3         intent.setDataAndType(data, "application/vnd.android.package-archive");  //Type的字符串為固定內(nèi)容
4         startActivity(intent);


注:第2行的路徑不能寫成:"file:///storage/sdcard0/···",不然報錯如下:



疑問:通過下面的這種方式安裝程序,運行時為什么會出錯呢?


 


復(fù)制代碼

復(fù)制代碼

1     //通過指定的action來安裝程序
2     public void installClickTwo(View view){
3         Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED);
4         Uri data = Uri.fromFile(new File("/storage/sdcard0/AndroidTest/smyh006_Intent01.apk"));    //路徑不能寫成:"file:///storage/sdcard0/···"
5         intent.setData(data);
6         startActivity(intent);
7     }

復(fù)制代碼

復(fù)制代碼

 


 


 


 


綜上所述,完整版代碼如下:


 


復(fù)制代碼

復(fù)制代碼

 1 <LinearLayout xmlns:android="http://schemas./apk/res/android"
 2     xmlns:tools="http://schemas./tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     android:orientation="vertical"
10     tools:context=".MainActivity" >
11     <Button 
12         android:id="@+id/button1_browsePage"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:onClick="browsePageClick"
16         android:text="打開指定網(wǎng)頁"/>   
17     <Button 
18         android:id="@+id/button2_openDialPage"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:onClick="openDialPageClick"
22         android:text="打開撥號面板"/>
23     <Button 
24         android:id="@+id/button3_dialPhone"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:onClick="dialPhoneClick"
28         android:text="直接撥打指定號碼"/>
29     <Button 
30         android:id="@+id/button4_openMsgPage"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:onClick="openMsgPageClick"
34         android:text="打開發(fā)短信的界面"/>
35     
36     
37      <Button 
38         android:id="@+id/button5_sendMsg"
39         android:layout_width="match_parent"
40         android:layout_height="wrap_content"
41         android:onClick="sendMsgClick"
42         android:text="給指定的人發(fā)短信"/>  
43         
44      <Button 
45         android:id="@+id/button6_playMusic"
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content"
48         android:onClick="playMusicClick"
49         android:text="播放指定路徑音樂"/>      
50      
51      <Button 
52         android:id="@+id/button7_uninstall"
53         android:layout_width="match_parent"
54         android:layout_height="wrap_content"
55         android:onClick="uninstallClick"
56         android:text="卸載程序"/>    
57      <Button 
58         android:id="@+id/button8_install"
59         android:layout_width="match_parent"
60         android:layout_height="wrap_content"
61         android:onClick="installClick"
62         android:text="安裝程序"/>    
63         
64     
65 </LinearLayout>

復(fù)制代碼

復(fù)制代碼

MainActivity.java代碼如下:


 


復(fù)制代碼

復(fù)制代碼

 1 package com.example.m06intent01;
 2 import java.io.File;
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.net.Uri;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.View;
 9 public class MainActivity extends Activity {
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14     }
15     //打開指定網(wǎng)頁
16     public void browsePageClick(View view){
17         Intent intent = new Intent(Intent.ACTION_VIEW);
18         intent.setData(Uri.parse("http://www.baidu.com/"));
19         startActivity(intent);  
20         
21     } 
22     
23     //打開撥號面板
24     public void openDialPageClick(View view){
25         Intent intent = new Intent(Intent.ACTION_DIAL);
26         intent.setData(Uri.parse("tel:10086"));
27         startActivity(intent);        
28     }
29     
30     //直接撥打指定號碼
31     public void dialPhoneClick(View view){
32         Intent intent = new Intent(Intent.ACTION_CALL);
33         intent.setData(Uri.parse("tel:10086"));
34         startActivity(intent);        
35     }
36     
37     //打開發(fā)短信的界面:action+type
38     public void openMsgPageClick(View view){
39         Intent intent = new Intent(Intent.ACTION_VIEW);
40         intent.setType("vnd.android-dir/mms-sms");
41         intent.putExtra("sms_body", "具體短信內(nèi)容"); //"sms_body"為固定內(nèi)容
42         startActivity(intent);        
43     }   
44     
45     //打開發(fā)短信的界面(指定電話號碼):action+data
46     public void sendMsgClick(View view){
47         Intent intent = new Intent(Intent.ACTION_SENDTO);
48         intent.setData(Uri.parse("smsto:18780260012"));
49         intent.putExtra("sms_body", "具體短信內(nèi)容"); //"sms_body"為固定內(nèi)容        
50         startActivity(intent);        
51     }      
52     
53     //播放指定路徑音樂
54     public void playMusicClick(View view){
55         Intent intent = new Intent(Intent.ACTION_VIEW);
56         Uri uri = Uri.parse("file:///storage/sdcard0/平凡之路.mp3");  //路徑也可以寫成:"/storage/sdcard0/平凡之路.mp3"
57         intent.setDataAndType(uri, "audio/mp3"); //方法:Intent android.content.Intent.setDataAndType(Uri data, String type)
58         startActivity(intent);
59     } 
60     
61     //卸載某個應(yīng)用程序,根據(jù)包名來識別
62     public void uninstallClick(View view){
63         Intent intent = new Intent(Intent.ACTION_DELETE);
64         Uri data = Uri.parse("package:com.example.smyh006intent01");
65         intent.setData(data);
66         startActivity(intent);
67     } 
68     
69     //安裝某個應(yīng)用程序,根據(jù)apk的文件名來識別
70     public void installClick(View view){
71         Intent intent = new Intent(Intent.ACTION_VIEW);
72         Uri data = Uri.fromFile(new File("/storage/sdcard0/AndroidTest/smyh006_Intent01.apk"));    //路徑不能寫成:"file:///storage/sdcard0/···"
73         intent.setDataAndType(data, "application/vnd.android.package-archive");  //Type的字符串為固定內(nèi)容
74         startActivity(intent);
75     }
76     
77     
78     @Override
79     public boolean onCreateOptionsMenu(Menu menu) {
80         // Inflate the menu; this adds items to the action bar if it is present.
81         getMenuInflater().inflate(R.menu.main, menu);
82         return true;
83     }
84     
85 }

復(fù)制代碼

復(fù)制代碼

運行后,主界面如下:


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多