本文主要是介绍发送短信并存入短信库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(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" />
这篇关于发送短信并存入短信库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!