Android中通過Intent調用其他應用的方法

 
啟動瀏覽器
代碼與范例:
 
[java] 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"https://www.google.com"</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span>   
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動瀏覽器並前往https://www.google.com 。
啟動撥號程序
代碼與范例:
 
[java] 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"tel:138"</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動撥號程序並輸入號碼138。
通話
代碼與范例:
 
[java] 
Uri uri<span style="color:#339933">=</span>Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"tel:138"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_CALL</span>,uri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:撥打號碼138。需要為程序申請權限<uses-permission android:name=”android.permission.CALL_PHONE”/>。
發送短信
代碼與范例:
 
[java] 
Uri smsUri <span style="color:#339933">=</span> Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span>url<span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, smsUri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setType</span><span style="color:#009900">(</span><span style="color:#0000ff">"vnd.android-dir/mms-sms"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動短信程序。
啟動通訊錄
代碼與范例:
 
[java] 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"content://contacts/people/1"</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動同學錄的某一條目。
啟動地圖程序(Google Maps等)
代碼與范例:
 
[java] 
Uri mapUri <span style="color:#339933">=</span> Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span>url<span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, mapUri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動地圖程序。
搜索路線
代碼與范例:
 
[java] 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setAction</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setClassName</span><span style="color:#009900">(</span><span style="color:#0000ff">"com.google.android.apps.maps"</span>,<span style="color:#0000ff">"com.google.android.maps.MapsActivity"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setData</span><span style="color:#009900">(</span>Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"https://maps.google.com/maps?myl=saddr&amp;daddr=&amp;dirflg=d"</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動Google Maps並搜索路線,具體的方法另外補充。
啟動郵件程序
代碼與范例:
 
[java] 
Uri uri <span style="color:#339933">=</span>Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"mailto:xxx@example.com"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent <span style="color:#339933">=</span> newIntent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_SENDTO</span>, uri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_SUBJECT</span>, <span style="color:#0000ff">"Hello world"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_TEXT</span>, <span style="color:#0000ff">"Ganbarimasu"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動郵件程序並將收件人設為xxx@example.com,郵件主題設為Hello world,內容設為Ganbarimasu。
啟動郵件程序並添加多個收件人
代碼與范例:
 
[java]
Intent intent<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_SEND</span><span style="color:#009900">)</span><span style="color:#339933">;</span>      
<a href="https://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399">String</span></a><span style="color:#009900">[</span><span style="color:#009900">]</span> tos<span style="color:#339933">=</span><span style="color:#009900">{</span><span style="color:#0000ff">"me@example.com"</span><span style="color:#009900">}</span><span style="color:#339933">;</span>      
<a href="https://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399">String</span></a><span style="color:#009900">[</span><span style="color:#009900">]</span>ccs<span style="color:#339933">=</span><span style="color:#009900">{</span><span style="color:#0000ff">"you@example.com"</span><span style="color:#009900">}</span><span style="color:#339933">;</span>      
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_EMAIL</span>, tos<span style="color:#009900">)</span><span style="color:#339933">;</span>      
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_CC</span>, ccs<span style="color:#009900">)</span><span style="color:#339933">;</span>      
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_TEXT</span>, <span style="color:#0000ff">"The email body text"</span><span style="color:#009900">)</span><span style="color:#339933">;</span>      
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_SUBJECT</span>, <span style="color:#0000ff">"The email subject text"</span><span style="color:#009900">)</span><span style="color:#339933">;</span>      
intent.<span style="color:#006633">setType</span><span style="color:#009900">(</span><span style="color:#0000ff">"message/rfc822"</span><span style="color:#009900">)</span><span style="color:#339933">;</span>      
startActivity<span style="color:#009900">(</span>Intent.<span style="color:#006633">createChooser</span><span style="color:#009900">(</span>intent,<span style="color:#0000ff">"Choose Email Client"</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動郵件程序並設置為發送給多個收件人。
啟動郵件程序並添加附件
代碼與范例:
1
2
3
4
5
[java]
Intent intent <span style="color:#339933">=</span> newIntent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_SEND</span><span style="color:#009900">)</span><span style="color:#339933">;</span>    
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_SUBJECT</span>, <span style="color:#0000ff">"The email subject text"</span><span style="color:#009900">)</span><span style="color:#339933">;</span>     
intent.<span style="color:#006633">putExtra</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">EXTRA_STREAM</span>,<span style="color:#0000ff">"file:///sdcard/mysong.mp3);    
sendIntent.setType("</span>audio<span style="color:#339933">/</span>mp3<span style="color:#0000ff">");    
startActivity(Intent.createChooser(intent,"</span>Choose Email Client<span style="color:#0000ff">"));</span> 
說明:啟動郵件程序並添加附件。
播放MP3文件
代碼與范例:
1
2
3
4
[java] 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
Uri uri <span style="color:#339933">=</span>Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"file:///sdcard/song.mp3"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setDataAndType</span><span style="color:#009900">(</span>uri,<span style="color:#0000ff">"audio/mp3"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動音樂程序並播放MP3歌曲。
卸載程序
代碼與范例:
1
2
3
[java] 
Uri uri <span style="color:#339933">=</span>Uri.<span style="color:#006633">fromParts</span><span style="color:#009900">(</span><span style="color:#0000ff">"package"</span>, strPackageName, <span style="color:#000066; font-weight:bold">null</span><span style="color:#009900">)</span><span style="color:#339933">;</span>    
Intent intent <span style="color:#339933">=</span> newIntent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_DELETE</span>, uri<span style="color:#009900">)</span><span style="color:#339933">;</span>    
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:卸載包名為strPackageName的程序。
安裝程序
代碼與范例:
1
2
[java]
Uri installUri <span style="color:#339933">=</span> Uri.<span style="color:#006633">fromParts</span><span style="color:#009900">(</span><span style="color:#0000ff">"package"</span>,strPackageName, <span style="color:#000066; font-weight:bold">null</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
returnIt <span style="color:#339933">=</span> newIntent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_PACKAGE_ADDED</span>, installUri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:安裝包名為strPackageName的程序。
啟動設置
代碼與范例:
1
2
[java]
Intent intent<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span><span style="color:#0000ff">"android.settings.SETTINGS"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:進入設定程序。
啟動電子市場
代碼與范例:
1
2
3
[java] 
Uri uri<span style="color:#339933">=</span>Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"market://search?q=pname:org.breezesoft.techolite"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent<span style="color:#339933">=</span><span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>,uri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:以包名org.breezesoft.techolite為條件啟動Android Market。
啟動電子市場並進入程序信息界面
代碼與范例:
1
2
3
[java]
Uri uri <span style="color:#339933">=</span> Uri.<span style="color:#006633">parse</span><span style="color:#009900">(</span><span style="color:#0000ff">"market://details?id=org.breezesoft.techolite"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_VIEW</span>, uri<span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivity<span style="color:#009900">(</span>intent<span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動Android Market並進入包名為org.breezesoft.techolite的程序的詳細信息界面。
從圖庫(Gallery)中選擇並獲取一張圖片
代碼與范例:
1
2
3
4
[java]
Intent intent <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Intent<span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setType</span><span style="color:#009900">(</span><span style="color:#0000ff">"image/*"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
intent.<span style="color:#006633">setAction</span><span style="color:#009900">(</span>Intent.<span style="color:#006633">ACTION_GET_CONTENT</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
startActivityForResult<span style="color:#009900">(</span>intent, <span style="color:#cc66cc">11</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
說明:啟動圖庫並中選擇並獲取一張圖片,返回原程序。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。