展会信息港展会大全

Android新手入门教程(十六):使用Intent调用“内置”应用
来源:互联网   发布日期:2016-01-14 09:34:54   浏览:1484次  

导读:我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内...

我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内置”应用。所谓的“内置”应用,指的就是系同级别的应用,比如Browser,Phone,Sms等等。举个例子,如果你的应用需要打开一个网页,可以使用Intent对象去调用浏览器,浏览器把网页显示出来,而不是要自己创建一个浏览器。。。

下面的例子展示如何调用系统中的几个比较常用的“内置”应用。

1.创建一个叫Intents的工程。

工程目录:

2.main.xml中的代码。

[java] <?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>

<?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] package net.learn2develop.Intents;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

public 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);

}

}

package net.learn2develop.Intents;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

public 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按钮:

其他的例子大家自己尝试吧。下次教程再详细解说Intents的用法。晚安,看书去了~~

摘自horsttnann的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港