Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面
作者:霸道流氓 时间:2021-09-04 15:39:22
场景
点击拨打电话按钮,跳转到拨打电话页面
点击发送短信按钮,跳转到发送短信页面
注:
实现
将布局改为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属性实现点击按钮跳转到拨打电话和发送短信界面网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/badaoliumangqizhi/archive/2020/01/08/12168987.html
标签:android,按钮,跳转,data,属性
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity Shader实现玻璃材质效果
2021-10-27 05:25:29
![](https://img.aspxhome.com/file/2023/7/77227_0s.jpg)
一次Jvm old过高的排查过程实战记录
2023-05-07 23:33:49
![](https://img.aspxhome.com/file/2023/1/87351_0s.jpg)
spring boot下 500 404 错误页面处理的方法
2021-09-03 07:12:36
Javaweb开发环境Myeclipse6.5 JDK1.6 Tomcat6.0 SVN1.8配置教程
2023-11-15 21:47:05
![](https://img.aspxhome.com/file/2023/1/73091_0s.png)
springboot整合JSR303校验功能实现代码
2023-10-11 21:46:25
![](https://img.aspxhome.com/file/2023/8/72378_0s.jpg)
C#使用Twain协议实现扫描仪连续扫描功能
2023-02-15 18:14:38
Spring JPA联表查询之OneToMany源码解析
2023-12-25 06:10:47
C#编程实现取整和取余的方法
2023-09-09 22:19:26
Socket结合线程池使用实现客户端和服务端通信demo
2023-01-21 20:23:50
![](https://img.aspxhome.com/file/2023/5/86835_0s.jpg)
在C#中使用OpenCV(使用OpenCVSharp)的实现
2023-02-21 16:15:38
![](https://img.aspxhome.com/file/2023/6/92956_0s.jpg)
浅谈两个jar包中包含完全相同的包名和类名的加载问题
2023-04-13 04:47:02
C# 得到某一天的起始和截止时间的代码
2021-11-13 10:57:54
C# 通过反射初探ORM框架的实现原理(详解)
2023-08-30 20:05:30
Jenkins初级应用之Invoke Phing targets插件配置
2023-10-14 03:55:56
![](https://img.aspxhome.com/file/2023/9/64049_0s.png)
Java WebService 简单实例(附实例代码)
2023-01-25 07:52:18
![](https://img.aspxhome.com/file/2023/8/64528_0s.png)
深入剖析Java工厂模式让你的代码更灵活
2022-05-26 00:42:28
详细解读Java的Lambda表达式
2021-12-30 15:32:36
![](https://img.aspxhome.com/file/2023/0/71650_0s.jpg)
eclipse创建java项目并运行的详细教程讲解
2022-09-27 20:15:13
![](https://img.aspxhome.com/file/2023/0/101970_0s.jpg)
Android利用RecyclerView实现全选、置顶和拖拽功能示例
2023-06-05 18:21:22
![](https://img.aspxhome.com/file/2023/8/117148_0s.gif)
Java中synchronized关键字引出的多种锁 问题
2021-06-05 04:42:45
![](https://img.aspxhome.com/file/2023/9/99929_0s.jpg)