场景

点击拨打电话按钮,跳转到拨打电话页面

点击发送短信按钮,跳转到发送短信页面

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加id属性。

然后添加两个按钮,并设置Id属性与显示文本。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".IntentActivity"> <Button
android:id="@+id/call"
android:text="拨打电话"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <Button
android:id="@+id/send"
android:text="发送短信"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>

然后来到Activity,首先通过ID获取者两个Button

        Button buttonCall = (Button) findViewById(R.id.call);
Button buttonSend = (Button) findViewById(R.id.send);

又因为这两个Button的点击事件监听器差不多,所有抽离出一个公共的点击事件监听器对象。

View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//将view强转为Button
Button button = (Button) v;
//根据button的id
switch(button.getId()){
//如果是拨打电话按钮
case R.id.call:
//设置Action行为属性
intent.setAction(intent.ACTION_DIAL);
//设置数据 后面123456789是默认要拨打的电话
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
break;
case R.id.send:
//设置行为为 发送短信
intent.setAction(intent.ACTION_SENDTO);
//设置发送至 10086
intent.setData(Uri.parse("smsto:10086"));
//设置短信的默认发送内容
intent.putExtra("sms_body","公众号:霸道的程序猿");
startActivity(intent);
break;
}
}
};

然后在OnCreate中对按钮设置点击事件监听器。

完整示例代码

package com.badao.relativelayouttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class IntentActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
Button buttonCall = (Button) findViewById(R.id.call);
Button buttonSend = (Button) findViewById(R.id.send);
buttonCall.setOnClickListener(listener);
buttonSend.setOnClickListener(listener);
} View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//将view强转为Button
Button button = (Button) v;
//根据button的id
switch(button.getId()){
//如果是拨打电话按钮
case R.id.call:
//设置Action行为属性
intent.setAction(intent.ACTION_DIAL);
//设置数据 后面123456789是默认要拨打的电话
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
break;
case R.id.send:
//设置行为为 发送短信
intent.setAction(intent.ACTION_SENDTO);
//设置发送至 10086
intent.setData(Uri.parse("smsto:10086"));
//设置短信的默认发送内容
intent.putExtra("sms_body","公众号:霸道的程序猿");
startActivity(intent);
break;
}
}
};
}

因为用到了打电话和发动短信,所以需要声明这两个权限,打开AndroidMainfest.xml

    <!--添加打电话权限-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!--添加发送短信权限-->
<uses-permission android:name="android.permission.SEND_SMS"/>

添加位置如下

Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信的更多相关文章

  1. Android 打开URL中的网页和拨打电话、发送短信功能

    拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...

  2. java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子

    一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...

  3. 脚本控制向Android模拟拨打电话,发送短信,定位设置功能

    做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost  5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...

  4. Android开发手记(15) 拨打电话和收发短信

    1.Intent简介 Android组价之间的通信,由Intent来协助完成.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...

  5. html5页面中 触发 拨打电话、发短信 的方式

    <a href="tel:18688888888">拨号</a> <a href="sms:18688888888">发短信 ...

  6. Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity

    一旦为Intent同时指定了Action.Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据类型执行相应的操作. 下面是几个Action属性.Data属性的组合. ...

  7. Android中的intent属性

    android之Intent的七大属性 2015年04月03日 ⁄ Android ⁄ 共 14866字 ⁄ 字号 小 中 大 ⁄ 1条评论 Intent用于封装程序的“调用意图”.两个Activit ...

  8. android使用Intent操作拨打号码发送短信

    Activity程序Activity.java package com.example.intentcaseproject; import android.net.Uri; import androi ...

  9. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

随机推荐

  1. Java数组合并方法学习。

    参考博客: https://blog.csdn.net/liu_005/article/details/72760392 https://blog.csdn.net/jaycee110905/arti ...

  2. Dart语言学习(六) Dart 列表List数组

    创建List : var list = [1,2,3,"Dart",true]; 创建不可变List : var list = const [1,2,3,"Dart&qu ...

  3. NIO&AIO编程模型

    NIO线程模型 什么是NIO线程模型? 上图是NIO的线程模型,  基于select实现,   这种线程模型的特点:  多条channel通过一个选择器和单挑线程绑定, 并且在这种编程模型中, Cha ...

  4. 「 神器 」快速启动应用Wox

    每天进步一丢丢,连接梦与想 合理的的要求是锻炼 不合理的要求是磨练 过分的要求是锤炼 今天分享一个会让你爱不释手的神器,Wox Wox 是一款国产开源免费的软件快捷启动工具,它可以快速搜索并打开你电脑 ...

  5. 使用luabind绑定box2d的lua接口

    最近在使用luabind绑定box2d的lua接口,发现不少问题.写在这里与大家分享. 1. body,fixture,joint的userdata.box2d的userdata的数据类型是void* ...

  6. Day4-Python3基础-装饰器、迭代器

    今日内容: 1.高阶函数 2.嵌套函数 3.装饰器 4.生成器 5.迭代器 1.高阶函数 定义: a:把一个函数名当作实参传给函数 a:返回值包含函数名(不修改函数的调用方式) import time ...

  7. 最短路径-Dijkstra+Floyd+Spfa

    Dijkstra算法: Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra ...

  8. Shell环境变量文件

    /etc/profile 系统级的初始化环境变量文件,由登录Shell调用执行 /etc/profile.d 当/etc/profile运行时,会调用该目录下的一些脚本 /etc/bashrc 每个交 ...

  9. Hibernate(五)

    ================================criteria(QBC)查询========================QBC,(容器)又名对象查询:采用对象的方式(主要是cri ...

  10. 9. Palindrome Number QuestionEditorial Solution

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...