服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面

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

2022-11-14 14:27霸道流氓 Android

这篇文章主要介绍了Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信,需要的朋友可以参考下

场景

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

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

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

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

注:

实现

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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中对按钮设置点击事件监听器。

完整示例代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

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

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

总结

以上所述是小编给大家介绍的Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/badaoliumangqizhi/archive/2020/01/08/12168987.html

延伸 · 阅读

精彩推荐