Android開發:顯式/隱式Intent


顯式跳轉

是在已知包名和類名的情況下常用的跳轉方法:

 

[java]  view plain  copy
 
  1. Intent mIntent = new Intent();  
  2. mIntent.setClassName("com.android.settings","com.android.settings.Settings");  
  3. mContext.startActivity(mIntent);  

 

我們也常這么用:

 

[java]  view plain  copy
 
  1. Intent intent = new Intent(mContext, XXActivity.class);  
  2. startActivity(intent);  

這是跳轉到當前應用的某個Activity,相信大家都十分熟悉,今天主要講的是如何使用隱式intent意圖跳轉

隱式跳轉

1、隱式跳轉之Action跳轉

假定有一個Activity在清單中是這樣的聲明的:
[html]  view plain  copy
 
  1. <activity android:name=".ActionActivity";   
  2.      <intent-filter  
  3.          action android:name="customer_action_here"  
  4.      </intent-filter>  
  5.  </activity>  
 
那么我們就可以使用以下代碼進行跳轉到上面這個Activity中:
[java]  view plain  copy
 
  1. //創建一個隱式的 Intent 對象:Action 動作  
  2. Intent intent = new Intent();  
  3. //設置 Intent 的動作為清單中指定的action  
  4. intent.setAction("customer_action_here");  
  5. startActivity(intent);  
 

2、隱式跳轉之Category跳轉

假定有一個Activity在清單中是這樣聲明的:
[html]  view plain  copy
 
  1. <activity android:name=".CategoryActivity" >  
  2.     <intent-filter>  
  3.         <action android:name="customer_action_here" />  
  4.         <category android:name="customer_category_here" />  
  5.     </intent-filter>  
  6. </activity>  

我們可以使用如下代碼進行跳轉到以上Activity:
[java]  view plain  copy
 
  1. //創建一個隱式的 Intent 對象:Category 類別  
  2. Intent intent = new Intent();  
  3. intent.setAction("customer_action_here");  
  4. //添加與清單中相同的自定義category  
  5. intent.addCategory("customer_category_here");  
  6. startActivity(intent);  

3、隱式跳轉之Data跳轉

假定有一個Activity是這樣定義的:
[html]  view plain  copy
 
  1. activity android:name=".DataActivity">  
  2.     intent-filter>  
  3.         category android:name="android.intent.category.DEFAULT" />  
  4.         data  
  5.             android:scheme="content"  
  6.             android:host="com.example.intentdemo"  
  7.             android:port="8080"  
  8.             android:pathPattern=".*pdf"  
  9.             android:mimeType="text/plain"/>  
  10.     < /intent-filter>  
  11. < /activity>  

我們可以使用如下代碼進行跳轉:
[java]  view plain  copy
 
  1. //創建一個隱式的 Intent 對象,方法四:Date 數據  
  2. Intent intent = new Intent();  
  3. Uri uri = Uri.parse("content://com.example.intentdemo:8080/abc.pdf");  
  4. //注:setData、setDataAndType、setType 這三種方法只能單獨使用,不可共用                 
  5. //單獨以 setData 方法設置 URI  
  6. //intent.setData(uri);  
  7. //單獨以 seType 方法設置 Type  
  8. //intent.setType("text/plain");  
  9. //上面分步驟設置是錯誤的,要么以 setDataAndType 方法設置 URI 及 mime type  
  10. intent.setDataAndType(uri, "text/plain");  
  11. startActivity(intent);  
 
清單中的port及以下屬性時可選的,沒有必要一定添加,但是添加了port及以下屬性的話,java代碼中的Uri中要做相應的匹配。
 

4、隱式跳轉之調用系統應用

4.1 使用瀏覽器瀏覽網頁

[java]  view plain  copy
 
  1. //web瀏覽器  
  2. Uri uri= Uri.parse("http://www.baidu.com");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(intent);  

4.2 調用地圖

[java]  view plain  copy
 
  1. //打開地圖查看經緯度  
  2. Uri uri = Uri.parse("geo:38.899533,-77.036476");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(intent);  

4.3 調用電話撥號(不需要撥號權限)

[java]  view plain  copy
 
  1. Uri uri = Uri.parse("tel:10086");  
  2. Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意區別於下面4.4的action  
  3. startActivity(intent);  

4.4 調用電話直接撥號(需要撥號權限)

[java]  view plain  copy
 
  1. Uri uri = Uri.parse("tel:15980665805");  
  2. Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意區別於上面4.3的aciton  
  3. startActivity(intent);  

4.5 調用短信程序(無需發送短信權限,接收者自填)

[java]  view plain  copy
 
  1. Intent intent = new Intent(Intent.ACTION_VIEW);      
  2. intent.putExtra("sms_body", "這里寫短信內容");      
  3. intent.setType("vnd.android-dir/mms-sms");      
  4. startActivity(intent);  

4.6 調用短信程序(無需發送短信權限)

[java]  view plain  copy
 
  1. Uri uri = Uri.parse("smsto:10086");//指定接收者  
  2. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);  
  3. intent.putExtra("sms_body", "你這個黑心運營商");  
  4. startActivity(intent);  

4.7 調用郵件程序

[java]  view plain  copy
 
  1. Intent intent = new Intent(Intent.ACTION_SENDTO);   
  2. intent.setData(Uri.parse("mailto:xxx@gmail.com"));   
  3. intent.putExtra(Intent.EXTRA_SUBJECT, "這是標題");   
  4. intent.putExtra(Intent.EXTRA_TEXT, "這是內容");   
  5. startActivity(intent);  

4.8 調用音樂播放器

[java]  view plain  copy
 
  1. Intent intent = new Intent(Intent.ACTION_VIEW);  
  2. //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  
  3. Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  
  4. intent.setDataAndType(uri, "audio/mp3");  
  5. startActivity(intent);  

4.9 調用視頻播放器

[java]  view plain  copy
 
  1. Intent intent = new Intent(Intent.ACTION_VIEW);  
  2. //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  
  3. Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  
  4. intent.setDataAndType(uri, "video/mp4");  
  5. startActivity(intent);  
 
調用視頻播放器和音樂播放器的區別在setDataAndType()時一個是audio類型,一個是video類型,很容易記住,不允許使用其他意思相近的單詞代替,代替無效。

4.10 調用搜索

[java]  view plain  copy
 
  1. Intent intent = new Intent();   
  2. intent.setAction(Intent.ACTION_WEB_SEARCH);   
  3. intent.putExtra(SearchManager.QUERY, "android");   
  4. startActivity(intent);  

總結

相信大家經過上面的介紹,已經對Intent跳轉有了一個比較成熟的理解,Intent是組件之間的紐帶,使用它可以讓系統替我們完成很多工作,不需要我們來指定完成工作的程序。實際上,我們會發現,調用系統程序使用液無非是隱式跳轉,只不過使用的是系統內置的一些Action,Uri,Data等信息而已。希望對大家有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM