当前位置:编程学习 > Android >>

【Android】批量短信发送模块SmsManager(短信管理器)

Android中的SmsManager(短息管理器),见名知意,就是用来管理手机短信的, 而该类的应用场景并不多。

发短信,例如给过节给朋友发送祝福,公司给客户发送节日问候,使用的场景还是比较多

如果您想使用现成的app,短信发送gotest安卓app联系QQ:7477118


调用系统发送短信功能:

Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("smsto:" + telephone));
sendIntent.putExtra("sms_body", "短信内容");
context.startActivity(sendIntent);


调用系统提供的短信接口发送短信

需要发短信的权限 

<uses-permissionandroid:name="android.permission.SEND_SMS"/>


直接调用SmsManager为我们提供的短信接口发送短信: 

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliverIntent);


参数说明: 

destinationAddress:收信人的电话号码 

scAddress:短信中心的号码,null的话使用当前默认的短信服务中心 

text:短信内容 

sentIntent:短信发送状态的信息:(发送状态的Intent) 如果不为null,当消息成功发送或失败这个PendingIntent就广播。结果代码是Activity.RESULT_OK 表示成功,或RESULT_ERROR_GENERIC_FAILURE、RESULT_ERROR_RADIO_OFF、RESULT_ERROR_NULL_PDU 之一表示错误。对应RESULT_ERROR_GENERIC_FAILURE,sentIntent可能包括额外的"错误代码"包含一 个无线电广播技术特定的值,通常只在修复故障时有用。每一个基于SMS的应用程序控制检测sentIntent。如果sentIntent是空,调用者将检测所有未知的应用程序,这将导致在检测的时候发送较小数量的SMS。 

deliverIntent:短信是否被对方收到的状态信息:(接收状态的Intent) 如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播, 状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据("pdu")

public void sendSMS(String phoneNumber,String message){
    //获取短信管理器
    android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
    //拆分短信内容(手机短信长度限制),貌似长度限制为140个字符,就是
    //只能发送70个汉字,多了要拆分成多条短信发送
    //第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null
    List<String> divideContents = smsManager.divideMessage(message);
    for (String text : divideContents) {
        smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);
    }
}
1)处理返回发送状态的sentIntent

//处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
//注册发送信息的广播接收者
context.registerReceiver(new BroadcastReceiver() {
    @Override  
    public void onReceive(Context _context, Intent _intent) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
            Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();
            break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: //普通错误
            break;
        case SmsManager.RESULT_ERROR_RADIO_OFF: //无线广播被明确地关闭
            break;
        case SmsManager.RESULT_ERROR_NULL_PDU: //没有提供pdu
            break;
        case SmsManager.RESULT_ERROR_NO_SERVICE: //服务当前不可用
            break;
        }
    }
}, new IntentFilter(SENT_SMS_ACTION));
2)处理返回接收状态的deliverIntent:

//处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
//创建接收返回的接收状态的Intent
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,deliverIntent, 0);
context.registerReceiver(new BroadcastReceiver() {
   @Override  
   public void onReceive(Context _context, Intent _intent) {
       Toast.makeText(context,"收信人已经成功接收", Toast.LENGTH_SHORT).show();
   }
}, new IntentFilter(DELIVERED_SMS_ACTION));


读取手机短信

获取android手机短信需要在AndroidManifest.xml加权限:

<uses-permission android:name="android.permission.READ_SMS" />
获取短信只需要得到ContentResolver就行了,它的URI主要有:

content://sms/                所有短信 

content://sms/inbox        收件箱 

content://sms/sent          已发送 

content://sms/draft         草稿 

content://sms/outbox      发件箱 

content://sms/failed        发送失败 

content://sms/queued     待发送列表

SMS数据库中的字段如下:

id:短信序号,如100

thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的

address:发件人地址,即手机号,如+8613811810000

person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null

date:日期,long型,如1256539465022,可以对日期显示格式进行设置

protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信

read:是否阅读0未读,1已读

status:短信状态-1接收,0complete,64pending,128failed

type:短信类型1是接收到的,2是已发出

body:短信具体内容

service_center:短信服务中心号码编号,如+8613800755500


发短信,例如给过节给朋友发送祝福,公司给客户发送节日问候,使用的场景还是比较多

批量短信发送gotest安卓app联系QQ:7477118


示例:

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

public class SmsReadActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(this);
        tv.setText(getSmsInPhone());

        ScrollView sv = new ScrollView(this);
        sv.addView(tv);

        setContentView(sv);
    }

    public String getSmsInPhone() {
        final String SMS_URI_ALL = "content://sms/"; // 所有短信
        final String SMS_URI_INBOX = "content://sms/inbox"; // 收件箱
        final String SMS_URI_SEND = "content://sms/sent"; // 已发送
        final String SMS_URI_DRAFT = "content://sms/draft"; // 草稿
        final String SMS_URI_OUTBOX = "content://sms/outbox"; // 发件箱
        final String SMS_URI_FAILED = "content://sms/failed"; // 发送失败
        final String SMS_URI_QUEUED = "content://sms/queued"; // 待发送列表

        StringBuilder smsBuilder = new StringBuilder();

        try {
            Uri uri = Uri.parse(SMS_URI_ALL);
            String[] projection = new String[] { "_id", "address", "person",
                    "body", "date", "type", };
            Cursor cur = getContentResolver().query(uri, projection, null,
                    null, "date desc"); // 获取手机内部短信
            // 获取短信中最新的未读短信
            // Cursor cur = getContentResolver().query(uri, projection,
            // "read = ?", new String[]{"0"}, "date desc");
            if (cur.moveToFirst()) {
                int index_Address = cur.getColumnIndex("address");
                int index_Person = cur.getColumnIndex("person");
                int index_Body = cur.getColumnIndex("body");
                int index_Date = cur.getColumnIndex("date");
                int index_Type = cur.getColumnIndex("type");

                do {
                    String strAddress = cur.getString(index_Address);
                    int intPerson = cur.getInt(index_Person);
                    String strbody = cur.getString(index_Body);
                    long longDate = cur.getLong(index_Date);
                    int intType = cur.getInt(index_Type);

                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd hh:mm:ss");
                    Date d = new Date(longDate);
                    String strDate = dateFormat.format(d);

                    String strType = "";
                    if (intType == 1) {
                        strType = "接收";
                    } else if (intType == 2) {
                        strType = "发送";
                    } else if (intType == 3) {
                        strType = "草稿";
                    } else if (intType == 4) {
                        strType = "发件箱";
                    } else if (intType == 5) {
                        strType = "发送失败";
                    } else if (intType == 6) {
                        strType = "待发送列表";
                    } else if (intType == 0) {
                        strType = "所以短信";
                    } else {
                        strType = "null";
                    }

                    smsBuilder.append("[ ");
                    smsBuilder.append(strAddress + ", ");
                    smsBuilder.append(intPerson + ", ");
                    smsBuilder.append(strbody + ", ");
                    smsBuilder.append(strDate + ", ");
                    smsBuilder.append(strType);
                    smsBuilder.append(" ]\n\n");
                } while (cur.moveToNext());

                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                smsBuilder.append("no result!");
            }

            smsBuilder.append("getSmsInPhone has executed!");

        } catch (SQLiteException ex) {
            Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
        }

        return smsBuilder.toString();
    }

}


qq7477118祝福短信批量发送APP不需要注册码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,