Android uses the action and data properties of intent to click the button to jump to the interface of making phone calls and sending SMS

scene

Click the call button to jump to the call page

Click the send SMS button to jump to the send SMS page

Note:

realization

Change the layout to LinearLayout, set it to vertical layout through Android: orientation = "vertical" > and add ID attribute.

Then add two buttons and set the ID property and display text.

<?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>

Then come to the activity. First, get the two buttons from the ID

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

Because the click event listeners of the two buttons are similar, all pull out a public click event listener object.

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;
      }
    }
  };

Then set the click event listener for the button in oncreate.

Complete sample code

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;
      }
    }
  };
}

Because you need to make a phone call and start a text message, you need to declare these two permissions and open androidmainfest.xml

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

summary

The above is what Xiaobian introduced to you. Android uses the action and data attributes of intent to click the button to jump to the interface of making phone calls and sending text messages. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>