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>
相关推荐
DongGei2 小时前
安卓-音频焦点
android·微信·音视频
冬田里的一把火32 小时前
[Android] [SnapdragonCamera] 单摄(横屏)阶段总结
android·数码相机
夏目艾拉3 小时前
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
android·java·flutter·设计模式
似霰3 小时前
安卓源码libvulkan“ depends on undefined module “libgpud_sys“报错解决
android
Arms2065 小时前
Android Fragment 学习备忘
android·学习
落魄的Android开发5 小时前
Android 跳转至各大应用商店应用详情页
android
技术无疆6 小时前
DDComponentForAndroid:探索Android组件化方案
android·java·开源·android-studio·组件化
繁依Fanyi7 小时前
【Python 千题 —— 算法篇】数字反转
android·java·开发语言·python·算法·eclipse·tomcat
人民的石头8 小时前
Android 12 SystemUI下拉状态栏禁止QuickQSPanel展开
android
aaajj8 小时前
自制游戏手柄--Android画面的input输入控制
android·游戏