【Android 开发教程】使用Intent调用内置应用程序
2012-05-17 17:40
507 查看
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内置”应用。所谓的“内置”应用,指的就是系同级别的应用,比如Browser,Phone,Sms等等。举个例子,如果你的应用需要打开一个网页,可以使用Intent对象去调用浏览器,浏览器把网页显示出来,而不是要自己创建一个浏览器。。。
下面的例子展示如何调用系统中的几个比较常用的“内置”应用。
1. 创建一个工程,Intents。
2. main.xml中的代码。
[java] view plaincopy<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" />
<Button
android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickShowMap"
android:text="Show Map" />
<Button
android:id="@+id/btn_launchMyBrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLaunchMyBrowser"
android:text="Launch My Browser" />
</LinearLayout>
3. IntentsActivity.java中的代码。[java] view plaincopypublic class IntentsActivity extends Activity {
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickWebBrowser(View view) {
Intent i = new Intent("android.intent.action.VIEW");
i.setData(Uri.parse("http://www.amazon.com"));
startActivity(i);
}
public void onClickMakeCalls(View view) {
Intent i = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567"));
startActivity(i);
}
public void onClickShowMap(View view) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:37.827500,-122.481670"));
startActivity(i);
}
}
4. 调试。
程序启动之后:
点击WebBrowser按钮:
点击MakeCalls按钮:
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内置”应用。所谓的“内置”应用,指的就是系同级别的应用,比如Browser,Phone,Sms等等。举个例子,如果你的应用需要打开一个网页,可以使用Intent对象去调用浏览器,浏览器把网页显示出来,而不是要自己创建一个浏览器。。。
下面的例子展示如何调用系统中的几个比较常用的“内置”应用。
1. 创建一个工程,Intents。
2. main.xml中的代码。
[java] view plaincopy<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" />
<Button
android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickShowMap"
android:text="Show Map" />
<Button
android:id="@+id/btn_launchMyBrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLaunchMyBrowser"
android:text="Launch My Browser" />
</LinearLayout>
3. IntentsActivity.java中的代码。[java] view plaincopypublic class IntentsActivity extends Activity {
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickWebBrowser(View view) {
Intent i = new Intent("android.intent.action.VIEW");
i.setData(Uri.parse("http://www.amazon.com"));
startActivity(i);
}
public void onClickMakeCalls(View view) {
Intent i = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567"));
startActivity(i);
}
public void onClickShowMap(View view) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:37.827500,-122.481670"));
startActivity(i);
}
}
4. 调试。
程序启动之后:
点击WebBrowser按钮:
点击MakeCalls按钮:
相关文章推荐
- Android开发之使用意图调用内置应用程序
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.2浅析Intent
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.4添加Category属性
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.3配置Intent-Filter
- Android 程式开发:(四)使用Intent调用“内置”应用 —— 4.1范例
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- Android开发:使用Intent打开电话、短信、邮箱、本地文件等系统应用程序整理大全
- 【Android 开发教程】使用Intent发送短信
- 【Android 开发教程】使用IntentService执行异步操作
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android---(4),Fragment的创建和使用,intent调用内置应用。
- android开发教程之获取使用当前api的应用程序名称
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- 【Android 开发教程】使用Intent传递数据
- Android开发:使用Intent打开电话、短信、邮箱、本地文件等系统应用程序整理大全
- Android开发教程:使用include调用布局
- 使用Intent调用内置应用程序