使用意图调用内置应用程序
1、创建一个新的Android项目并命名为Intents,在main.xml文件中添加两个Button:
<Button
android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickWebBrowser"
android:text="Web Browser" />
<Button
android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickMakeCalls"
android:text="Make Calls" />
2、在IntentsActivity.java文件中添加如下代码:
public void onClickWebBrowser(View v) {//浏览器
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://网址"));//网址处输入百度网址,CSDN不让直接写网址...
startActivity(intent);
}
public void onClickMakeCalls(View v) {//打电话
Intent intent = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567"));
startActivity(intent);
}
3、运行程序,效果如下:
点击Web Browser:
点击Make Calls:
总结:
在Android中,意图通常是成对出项:动作(Action)和数据(data)。动作描述了要执行什么,数据则指定了受影响的对象。
动作的一些示例:Action_VIEW、ACTION_DIAL、ACTION_PICK;
数据的一些示例:tel:+651234567、geo:37.827500,-122.481670、content://contacts。