Android 发送短信功能

Android 发送短信功能

Android 发送短信功能

输入短信内容和手机号码后,跳转到系统短信功能,然后点击发送即可

MainActivity.Java

java 复制代码
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;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText smsNumber;
    private EditText smsContent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        smsContent = (EditText) findViewById(R.id.sms_content);
        smsNumber = (EditText) findViewById(R.id.sms_number);

        Button smsButton = (Button) findViewById(R.id.sms_button);
        smsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSmsByIntent();
            }
        });
    }

    private void sendSmsByIntent() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.putExtra("sms_body", smsContent.getText().toString());
        Uri data = Uri.parse("smsto:" + smsNumber.getText().toString());
        intent.setData(data);
        startActivity(intent);
    }
}

activity_main.xml

xml 复制代码
<?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=".MainActivity">

    <EditText
        android:id="@+id/sms_content"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:gravity="top|left"
        android:hint="输入发送的内容"
        android:singleLine="false"
        android:textSize="22sp" />

    <EditText
        android:id="@+id/sms_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="输入号码"
        android:inputType="phone"
        android:singleLine="true"
        android:textSize="22sp" />

    <Button
        android:id="@+id/sms_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="发送" />

</LinearLayout>

AndroidManifest.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Learn">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
相关推荐
鹏多多9 分钟前
flutter睡眠与冥想数据可视化神器:sleep_stage_chart插件全解析
android·前端·flutter
勇气要爆发43 分钟前
【第五阶段-高级特性和架构】第三章:高级状态管理—GetX状态管理篇
android·架构
未来之窗软件服务3 小时前
服务器运维(十五)自建WEB服务C#PHP——东方仙盟炼气期
android·服务器运维·东方仙盟·东方仙盟sdk·自建web服务
Zender Han7 小时前
Flutter 新版 Google Sign-In 插件完整解析(含示例讲解)
android·flutter·ios·web
来来走走11 小时前
Android开发(Kotlin) LiveData的基本了解
android·开发语言·kotlin
。puppy12 小时前
MySQL 远程登录实验:通过 IP 地址跨机器连接实战指南
android·adb
dongdeaiziji12 小时前
深入理解 Kotlin 中的构造方法
android·kotlin
风起云涌~13 小时前
【Android】浅谈Navigation
android
游戏开发爱好者813 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
QuantumLeap丶15 小时前
《Flutter全栈开发实战指南:从零到高级》- 18 -自定义绘制与画布
android·flutter·ios