android自动发短信_Android

在Android中,可以使用SmsManager类实现自动发短信功能。首先需要获取短信管理器的实例,然后设置收件人、内容和发送方式,最后调用sendTextMessage方法发送短信。
android自动发短信_Android

Android自动发短信

准备工作

1、在AndroidManifest.xml文件中添加短信权限:

<usespermission android:name="android.permission.SEND_SMS" />

2、在布局文件(如activity_main.xml)中添加发送按钮和输入框:

<EditText
    android:id="@+id/editTextPhoneNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号码" />
<Button
    android:id="@+id/buttonSendSms"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发送短信" />

3、在MainActivity.java文件中获取输入框和发送按钮的引用:

EditText editTextPhoneNumber = findViewById(R.id.editTextPhoneNumber);
Button buttonSendSms = findViewById(R.id.buttonSendSms);

实现自动发短信功能

1、为发送按钮设置点击事件监听器:

buttonSendSms.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String phoneNumber = editTextPhoneNumber.getText().toString();
        if (!phoneNumber.isEmpty()) {
            sendSms(phoneNumber);
        } else {
            Toast.makeText(MainActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
        }
    }
});
android自动发短信_Android

2、编写sendSms方法,实现发送短信的功能:

private void sendSms(String phoneNumber) {
    String message = "这是一条自动发送的短信";
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    Toast.makeText(MainActivity.this, "短信已发送", Toast.LENGTH_SHORT).show();
}

3、在AndroidManifest.xml文件中添加短信管理器的权限:

<usespermission android:name="android.permission.RECEIVE_SMS" />

4、在MainActivity.java文件中注册短信接收器:

private final SmsReceiver smsReceiver = new SmsReceiver();
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private IntentFilter intentFilter;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    intentFilter = new IntentFilter(SMS_RECEIVED);
    registerReceiver(smsReceiver, intentFilter);
}
...
@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(smsReceiver);
}

5、编写短信接收器类SmsReceiver:

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                for (Object pdu : pdus) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                    String sender = smsMessage.getDisplayOriginatingAddress(); // 获取发送者号码
                    String body = smsMessage.getDisplayMessageBody(); // 获取短信内容
                    Toast.makeText(context, "收到短信:" + sender + " " + body, Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Toast.makeText(context, "未收到短信", Toast.LENGTH_SHORT).show();
        }
    }
}
android自动发短信_Android

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/521882.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seo的头像K-seoSEO优化员
Previous 2024-06-05 18:51
Next 2024-06-05 18:54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入