Android realizes the function of sending SMS and solves the permission problem

1. Create a project, create a new Activity
2. sendSmsActivity.java

package com.otis.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.otis.slamrobot.R;

public class SendSmsActivity extends AppCompatActivity {
    
    

    EditText phone, content;
    Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);
        ActivityCompat.requestPermissions(this,new String[]{
    
    Manifest.permission.SEND_SMS} , -1);
        // 获取SMSManager管理器
        final SmsManager smsManager = SmsManager.getDefault();
        //初始化控件
        phone = (EditText)findViewById(R.id.et_phone);
        content = (EditText)findViewById(R.id.et_content);
        send = (Button) findViewById(R.id.btn_send);

        send.setOnClickListener(new View.OnClickListener() {
    
    

            @Override
            public void onClick(View v) {
    
    
                String phoneNum = phone.getText().toString();
                String contentText = content.getText().toString();
                //创建一个android.app.PendingIntent对象
                PendingIntent pi = PendingIntent.getActivity(SendSmsActivity.this, 0, new Intent(), 0);
                //发送短信
                smsManager.sendTextMessage(phoneNum, null, contentText, pi, null);
                //提示短信发送完成
                Toast.makeText(SendSmsActivity.this, "短信发送完成", Toast.LENGTH_SHORT).show();
                System.out.println(phoneNum+contentText);
            }
        });
    }

}

3.AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ui.sendsmstest">

    <uses-permission android:name="android.permission.SEND_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name="com.ui.SendSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.activity_send_sms.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ui.SendSmsActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="收件人"/>

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送内容"/>

        <EditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="top"
            android:lines="5"
            android:text="你好"/>
    </LinearLayout>

    <Button
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:id="@+id/btn_send"
        />
</LinearLayout>

5. Regarding permissions: you
need to set it on your phone to open: Settings -> Permission Management -> Application -> Find the corresponding APP -> Trust this application

Guess you like

Origin blog.csdn.net/qq_43030934/article/details/108473523