Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信
场景
点击拨打电话按钮,跳转到拨打电话页面

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

注:
博客:
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属性实现点击按钮跳转到拨打电话和发送短信的更多相关文章
- Android 打开URL中的网页和拨打电话、发送短信功能
拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...
- java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子
一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...
- 脚本控制向Android模拟拨打电话,发送短信,定位设置功能
做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost 5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...
- Android开发手记(15) 拨打电话和收发短信
1.Intent简介 Android组价之间的通信,由Intent来协助完成.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...
- html5页面中 触发 拨打电话、发短信 的方式
<a href="tel:18688888888">拨号</a> <a href="sms:18688888888">发短信 ...
- Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity
一旦为Intent同时指定了Action.Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据类型执行相应的操作. 下面是几个Action属性.Data属性的组合. ...
- Android中的intent属性
android之Intent的七大属性 2015年04月03日 ⁄ Android ⁄ 共 14866字 ⁄ 字号 小 中 大 ⁄ 1条评论 Intent用于封装程序的“调用意图”.两个Activit ...
- android使用Intent操作拨打号码发送短信
Activity程序Activity.java package com.example.intentcaseproject; import android.net.Uri; import androi ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
随机推荐
- 深入学习MySQL 01 一条查询语句的执行过程
在学习SpringCloud的同时,也在深入学习MySq中,听着<mysql45讲>,看着<高性能MySQL>,本系列文章是本人学习过程的总结,水平有限,仅供参考,若有不对之处 ...
- java面试| 精选基础题(2)
关注微信公众号"java从心",置顶公众号 每天进步一点点,距离大腿又近一步! 阅读本文大概需要6分钟 继续挖掘一些有趣的基础面试题,有错望指出来哈,请赐教~ 1.包装类的装箱与拆 ...
- GitHub 上这几个沙雕项目,够我玩几天
在家里都憋坏了吧?每天睡了吃吃了睡,该找点事做做了,今天推荐几个好(沙)玩(雕)的开源项目,好在家打发时间. 91 吴先生 一个在线的 PornHub 风格 Logo 生成工具 Logoly.Pro ...
- pc端的弹性布局适配方案
方案及原理:使用rem单位,通过window.onresize来监听浏览器窗口,获取窗口宽度,并改变跟字体大小来达到弹性适配效果. function adaptor(){ //为了便于计算,这里以19 ...
- JS-05-元素获取
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ZJU-Reactor Cooling(无源汇有上下界最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 Reactor Cooling Time Limit: 5 ...
- Redis入门,Jedis和常用命令
一.Redis简介 1.关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库,最终会将数据持久化到磁盘上,而nosql数据 库是基于特殊的结构,并将数据存储到内存的数据库.从性 ...
- Informatica在linux下安装搭建
安装介质清单准备 介质名称 版本信息 描述 Informatica Powercenter 9.5.1 for Linux 64 bit 必须 Java Jdk 1.6.0_45 for Linux ...
- 大数据之Kafka史上最详细原理总结
Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实 ...
- Activity--Eclipse安装Activity designer插件失败
案例 今天使用Eclipse 安装Activity designer插件时,出现了如下错误: An error occurred while collecting items to be instal ...