发送短信并存入短信库

2024-05-07 03:32
文章标签 发送 短信 存入

本文主要是介绍发送短信并存入短信库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在使用SmsManager服务群发短信 一文中介绍过短信的发送,这里把短信存入数据库的代码补上,比较简单,直接上代码,里面有注释:

MainActivity:

package com.home.sendsms;import java.util.ArrayList;import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button sendBtn;private EditText numberText;private EditText contentText;private final String SEND_ACTION = "com.home.send";private final String RECEIVE_ACTION = "com.home.receive";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initWidget();}private void initWidget() {sendBtn = (Button) findViewById(R.id.main_btn);sendBtn.setOnClickListener(this);numberText = (EditText) findViewById(R.id.main_et_number);contentText = (EditText) findViewById(R.id.main_et_content);}@Overridepublic void onClick(View v) {if (v == sendBtn) {// 获取界面数据并验证String number = numberText.getText().toString();String content = contentText.getText().toString();if (TextUtils.isEmpty(number)) {Toast.makeText(this, "号码不能为空", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(content)) {Toast.makeText(this, "短信内容不能为空", Toast.LENGTH_SHORT).show();return;}// 注册广播registerReceiver(sendReceiver, new IntentFilter(SEND_ACTION));registerReceiver(receiveReceiver, new IntentFilter(RECEIVE_ACTION));// 发送sendSMS(number, content);// 将发送的短信插入短信库ContentValues values = new ContentValues();// 发送时间values.put("date", System.currentTimeMillis());// 阅读状态:0为未读 1为已读values.put("read", 0);// 类型:1为接收 2为发送values.put("type", 2);// 接收者号码values.put("address", number);// 短信内容values.put("body", content);// 插入短信库getContentResolver().insert(Uri.parse("content://sms"), values);}}/*** 发送短信* * @param number* @param message*/private void sendSMS(String number, String message) {SmsManager sms = SmsManager.getDefault();Intent sentIntent = new Intent(SEND_ACTION);PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,0);Intent receiveIntent = new Intent(RECEIVE_ACTION);PendingIntent receivePI = PendingIntent.getBroadcast(this, 0,receiveIntent, 0);// 如果短信内容超过70个字符则将这条短信拆成多条短信进行发送if (message.length() > 70) {ArrayList<String> msgs = sms.divideMessage(message);for (String msg : msgs) {sms.sendTextMessage(number, null, msg, sentPI, receivePI);}} else {sms.sendTextMessage(number, null, message, sentPI, receivePI);}}private BroadcastReceiver sendReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (getResultCode() == Activity.RESULT_OK) {Toast.makeText(context, "发送成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show();}}};private BroadcastReceiver receiveReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (getResultCode() == Activity.RESULT_OK) {Toast.makeText(context, "对方接收成功", Toast.LENGTH_SHORT).show();}}};@Overrideprotected void onDestroy() {unregisterReceiver(sendReceiver);unregisterReceiver(receiveReceiver);}
}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><EditTextandroid:id="@+id/main_et_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入接收者号码"android:numeric="integer"android:singleLine="true" /><EditTextandroid:id="@+id/main_et_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入短信内容" /><Buttonandroid:id="@+id/main_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="发送" /></LinearLayout>

权限:

    <!-- 发送消息 --><uses-permission android:name="android.permission.SEND_SMS" /><!-- 阅读消息 --><uses-permission android:name="android.permission.READ_SMS" /><!-- 写入消息 --><uses-permission android:name="android.permission.WRITE_SMS" /><!-- 接收消息 --><uses-permission android:name="android.permission.RECEIVE_SMS" />




 

这篇关于发送短信并存入短信库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966195

相关文章

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

python运用requests模拟浏览器发送请求过程

《python运用requests模拟浏览器发送请求过程》模拟浏览器请求可选用requests处理静态内容,selenium应对动态页面,playwright支持高级自动化,设置代理和超时参数,根据需... 目录使用requests库模拟浏览器请求使用selenium自动化浏览器操作使用playwright

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

使用Python和SQLAlchemy实现高效的邮件发送系统

《使用Python和SQLAlchemy实现高效的邮件发送系统》在现代Web应用中,邮件通知是不可或缺的功能之一,无论是订单确认、文件处理结果通知,还是系统告警,邮件都是最常用的通信方式之一,本文将详... 目录引言1. 需求分析2. 数据库设计2.1 User 表(存储用户信息)2.2 CustomerO

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾