Android学习笔记之使用意图打开内置应用程序组件

(1)布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="IntentTest"
        android:text="从Google搜索内容" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:onClick="IntentTest"
        android:text="游览网页" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:onClick="IntentTest"
        android:text="显示地图" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:onClick="IntentTest"
        android:text="拨打电话" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_centerVertical="true"
        android:onClick="IntentTest"
        android:text="发短信" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/button5"
        android:onClick="IntentTest"
        android:text="播放多媒体" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button4"
        android:layout_below="@+id/button6"
        android:onClick="IntentTest"
        android:text="安装APK" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button7"
        android:layout_below="@+id/button7"
        android:onClick="IntentTest"
        android:text="卸载APK" />

</RelativeLayout>

(2)MainActivity.java

package com.example.intent_openinterapplicationapi;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private Button button6;
	private Button button7;
	private Button button8;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		button3 = (Button) this.findViewById(R.id.button3);
		button4 = (Button) this.findViewById(R.id.button4);
		button5 = (Button) this.findViewById(R.id.button5);
		button6 = (Button) this.findViewById(R.id.button6);
		button7 = (Button) this.findViewById(R.id.button7);
		button8 = (Button) this.findViewById(R.id.button8);

	}

	public void IntentTest(View v) {
		switch (v.getId()) {
		case R.id.button1:
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_WEB_SEARCH);
			intent.putExtra(SearchManager.QUERY, "searchString");
			startActivity(intent);
			break;
		case R.id.button2:
			Uri uri = Uri.parse("http://www.baidu.com");
			Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent2);
			break;
		case R.id.button3:
			Uri uri1 = Uri.parse("geo:38.899533,77.036476");
			Intent intent3 = new Intent(Intent.ACTION_VIEW, uri1);
			startActivity(intent3);
			break;
		case R.id.button4:
			Uri uri2 = Uri.parse("tel:18080808080");
			Intent intent4 = new Intent(Intent.ACTION_DIAL, uri2);
			startActivity(intent4);
			break;
		case R.id.button5:
			Intent intent5 = new Intent(Intent.ACTION_VIEW);
			intent5.putExtra("sms_body", "The SMS TEXT!");
			intent5.setType("vnd.android-dir/mms-sms");
			startActivity(intent5);
			break;
		case R.id.button6:
			Intent intent6 = new Intent(Intent.ACTION_VIEW);
			Uri uri3 = Uri.parse("file:///sdcard/song.mp3");
			intent6.setDataAndType(uri3, "audio/mp3");
			startActivity(intent6);
			break;
		case R.id.button7:
			Uri installUri = Uri.fromParts("package", "xxx", null);
			Intent intent7 = new Intent(Intent.ACTION_PACKAGE_ADDED);
			startActivity(intent7);
			break;
		case R.id.button8:
			Uri deletellUri = Uri.fromParts("package", "strPackageName", null);
			Intent intent8 = new Intent(Intent.ACTION_DELETE, deletellUri);
			startActivity(intent8);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

时间: 2024-08-28 12:23:15

Android学习笔记之使用意图打开内置应用程序组件的相关文章

Android学习笔记(十七)——使用意图调用内置应用程序

使用意图调用内置应用程序 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&quo

JSP学习笔记(3)-JSP内置对象

有些对象不用声明就可以在JSP页面的Java程序片和表达式部分使用,这些对象就是JSP的内置对象.  JSP常用的内置对象有request,response,session,application,out.  其中两个重要的request和response对象提供了服务器和浏览器通信方法的控制. 1.request对象(请求对象) 作用:封装用户提交的信息,获取封装信息等 1.1.获取用户提交信息 request.getParameter(String s);//获取提交信息    如果该方法获

Android学习笔记之广播意图及广播接收者MyBroadcastReceiver、Broadcast

(1)第一种使用xml文件进行注册 布局文件,添加一个button点击的时候进行广播 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height

Bootstrap学习笔记(七)其它内置组件

缩略图(一) 缩略图在网站中最常用的地方就是产品列表页面,一行显示几张图片,有的在图片底下(左侧或右侧)带有标题.描述等信息.Bootstrap框架将这一部独立成一个模块组件.并通过"thumbnail"样式配合bootstrap的网格系统来实现.可以将产品列表页变得更好看. 源码文件: ?  LESS版本:对应文件thumbnails.less ?  Sass版本:对应文件_thumbnails.scss ? 编译后版本:bootstrap.css文件第4402行-第4426行 使用

python学习笔记-Day04-第三部分(内置函数,map,filter,reduce,yield)

map遍历序列,对其中的每个元素进行相应的操作,最终获得新的序列lst =[1,2,3,4]def fun(arg):    return arg+10new_lst = map(fun,lst) def fun2(a1,a2):    return a1+a2lst01 = [11,22,33]lst02 = [1,2,3]map(func2,lst01,lst02)  #序列的长度需要一致 或者 map(lambda a1,a2,a3:a1+a2,lst01,lst02) #########

Android学习(十) SQLite 基于内置函数的操作方式

main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:or

Hive 学习笔记(启动方式,内置服务)

一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令行接口(CLI):以命令行的形式输入SQL语句进行数据数据操作 Web界面:通过Web方式进行访问. http://p.baidu.com/itopic/main/qlog?qid=e0f26162633565663838623000&type=questionlog http://p.baidu.

Android学习笔记(十)——使用意图链接活动

使用意图链接活动 1.新建一个名为"UsingIntent"的项目,右击src文件夹下的包名,选择New-->Class选项,并将新的类文件名命名为"SecondActivity": 2.打开AndroidManifest.xml文件,添加如下代码: <activity android:name=".SecondActivity" android:label="Second Activity" > <!

Android学习笔记(十一)——从意图返回结果

从意图返回结果 startActivity()方法调用另一个活动,但并没有返回结果给当前活动.此时如想从一个活动中回传数据,就要使用startActivityForResult()方法. 点此获取完整代码~                                                                  1.使用上一篇中创建的项目,在secondactivity.xml文件中添加文本框和按钮,代码如下: <TextView android:layout_width