Intent意图打开电话、邮件、短信、地图、上网_xml

####1、界面显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lumeng.myapplication.MainActivity">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_call"
            android:text="@string/call"
            android:layout_marginTop="30dp"
            android:textSize="30dp"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_email"
            android:text="@string/email"
            android:textSize="30dp"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_mes"
            android:text="@string/mes"
            android:textSize="30dp"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_map"
            android:text="@string/map"
            android:textSize="30dp"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_wep"
            android:text="@string/wep"
            android:textSize="30dp"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/bt_search"
            android:text="@string/search"
            android:textSize="30dp"/>
    </LinearLayout>


</RelativeLayout>

####2、功能实现

package com.example.lumeng.myapplication;

import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button call,msg,email,map,wep;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        call = (Button) findViewById(R.id.bt_call);
        msg = (Button) findViewById(R.id.bt_mes);
        email = (Button) findViewById(R.id.bt_email);
        map = (Button) findViewById(R.id.bt_map);
        wep = (Button) findViewById(R.id.bt_wep);


        call.setOnClickListener(this);
        msg.setOnClickListener(this);
        email.setOnClickListener(this);
        map.setOnClickListener(this);
        wep.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id =view.getId();
        Intent it = new Intent();

        switch (id) {
            case  R.id.bt_call:
                it.setAction(Intent.ACTION_VIEW);//设置动作,显示数据,显示拨号界面
                //it.setAction(Intent.ACTION_CALL);//直接拨打
                it.setData(Uri.parse("tel:10086"));//设置uri指定电话号码
                break;

            case R.id.bt_email:
                it.setData(Uri.parse("mailto:service@flag.com.tw")); //收件人
                it.putExtra(Intent.EXTRA_CC,new String[] {"test@flag.com.tw"});//抄送人
                it.putExtra(Intent.EXTRA_SUBJECT,"数据已经收到");//设置主题
                it.putExtra(Intent.EXTRA_TEXT,"谢谢");//设置内容
                break;
            case R.id.bt_mes:
                it.setData(Uri.parse("sms:10086?body=话费"));//短信号码和内容
                break;

            case R.id.bt_wep:
                it.setData(Uri.parse("http://www.baidu.com/?from=wap")); //链接网址
                break;

            case R.id.bt_map:
                it.setData(Uri.parse("geo:39.896086,116.151147"));//地图坐标
                break;
            case R.id.bt_search:
                it.setAction(Intent.ACTION_SEARCH);//将动作改为搜索
                it.putExtra(SearchManager.QUERY,"长城");
                break;
            default:
                break;
        }
        startActivity(it);//寻找适合意图的程序,多个会出现选择。
    }
}

文献参考:
android app开发入门 施威铭 编著

本人郑重声明,本博客所著文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若以上文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。