Android 短信发送、短信保存至数据库

忙了这么长时间,毕业论文终于完成了,呵呵,现在就准备答辩了。然后,在平静中度过我最后的大学生活 哈哈,,,

这次介绍下有关短信的发送,在这里介绍的是发动普通的文本短信,还有一种是数据短信。

发送短信必须具有相应的权限:

1. 发送普通文本短信

SmsManager smsManager = SmsManager.getDefault();  //短信管理器
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,new Intent(), 0);
// 电话号码验证,简单的数据验证
for (int count = 0; count < length; count++) {
    Pattern pattern = Pattern.compile("^[0-9]+$");
    Matcher matcher = pattern.matcher(phoneNumber);
    if (!matcher.matches()) {
                              //弹出警告对话框
        showDialog(DIALOG_NUMBER_ERROR);
        return;
    }
}
for (int i = 0; i < length; i++) {
    if (smsContent.length() > 70) {
        // 短信内容的字数大于70
        List msgs = smsManager.divideMessage(smsContent);
        for (String msg : msgs) {
            smsManager.sendTextMessage(contactsNumber[i], null,msg, sentIntent, null);
        }
    } else {
        smsManager.sendTextMessage(contactsNumber[i], null,smsContent, sentIntent, null);
    }
    // 保存发送短信的内容
    ContentValues values = new ContentValues();
    values.put("address", contactsNumber[i]);
    values.put("body", smsContent);
    getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
Toast.makeText(this, R.string.sendSmsSuccess,Toast.LENGTH_SHORT).show();
2. 发送数据短信
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent("com.myself.action.SMS_SEND_RESULT");
PendingIntent dummySentEvent = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
Intent deliveryIntent = new Intent("com.myself.action.SMS_DELIVERY_RESULT");
PendingIntent dummyDeliveryEvent = PendingIntent.getBroadcast(this, 0, deliveryIntent, 0);
int port=51234;
byte[]  sms_data = myString.getBytes();
try {
   smsManager.sendDataMessage(telNumStr, null, port, sms_data,dummySentEvent, dummyDeliveryEvent);
} catch (Exception e) {
  Log.e("SmsSending", "SendException", e);
}
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
关于如何接收短信,也就是采用拦截广播方式BroadcastReceiver,二者在onReceive方法中的实现逻辑相似,

只是把getDisplayMessageBody()改成getUserData().

在AndroidManifest.xml文件中注册短信广播接收器时,有些差别。
 
 
接收文本短信的广播接收器的注册方式:
 
 
<<>uses-permission android:name="android.permission.RECEIVE_SMS" />
<<>receiver android:name="MessageDemo">
   <<>intent-filter>
     <<>action android:name="android.provider.Telephony.SMS_RECEIVED" /> intent-filter> receiver> 
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
 
 
接收数据短信的广播接收器的注册方式:
 
 
<<>uses-permission android:name="android.permission.RECEIVE_SMS" />
<<>receiver android:name="MessageDemo">
   <<>intent-filter>
      <<>action  android:name="android.intent.action.DATA_SMS_RECEIVED" />
      <<>data android:scheme="sms" />
      <<>data android:host="localhost" />
      <<>data android:port="51234" />
  intent-filter>
receiver>
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
 
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值