Android Intent of Action and use the Data property to achieve click the button to jump to make phone calls and send text messages

Scenes

Click the button to make a call, make a call to jump to page

 

 

Click the button to send text messages, send text messages to jump to page

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.

achieve

To the LinearLayout layout, and by android: orientation = "vertical"> vertical layout setting, and then add the id attribute.

Then add two buttons, and set the text display attributes 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"/>

 

 

添加位置如下

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/12168987.html