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">"http://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>
说明:启动浏览器并前往http://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"
补充:移动开发 , Android ,