Rexsee API介绍:SMSSender短信发送

 

rexseeSMSSender扩展,实现短信发送与事件回调。

 

【函数】 void send(String phoneNumber, String message)

【说明】 发送短信。

【返回】 无

【参数】

phoneNumber:目标手机号码。

message:短信内容。

 

【函数】 void send(String id, String phoneNumber, String message)

【说明】 发送短信并回调事件。

【返回】 无

【参数】

id:任意的ID,在事件onSmsSent和onSmsDelivered中标识该条短信,如果为空,则onSmsSent和onSmsDelivered不会被调用。

phoneNumber:目标手机号码。

message:短信内容。

 

【事件】 void onSmsSent(String id, String result)

【说明】 调用send()函数后,如果id不为空,当短信发送完毕时触发。

【参数】

id:短信的自定义id。

result:发送结果,如果成功返回"OK",否则返回错误信息。

 

 

【事件】 void onSmsDelivered(String id, String pdu)

【说明】 调用send()函数后,如果id不为空,当短信发送到接收方时触发,注意,触发该事件可能需要较长时间,取决于网络状况。

【参数】

id:短信的自定义id。

pdu:发送报告。

 

详细java源码:rexseeSMSSender.java

 

/* 
* Copyright (C) 2011 The Rexsee Open Source Project 
* 
* Licensed under the Rexsee License, Version 1.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*      http://www.rexsee.com/CN/legal/license.html 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
 
package rexsee.communication;  
 
import java.util.ArrayList;  
 
import rexsee.core.browser.Browser;  
import rexsee.core.browser.clazz.JavascriptInterface;  
import android.app.Activity;  
import android.app.PendingIntent;  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.content.IntentFilter;  
import android.telephony.SmsManager;  
 
public class RexseeSMSSender implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "SMSSender";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(Browser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(Browser childBrowser) {  
               return new RexseeSMSSender(childBrowser);  
       }  
 
       public static final String EXTRA_SMS_ID = "smsID";  
       public static final String EXTRA_SMS_PART = "smsPart";  
       public static final String EXTRA_SMS_PART_TOTAL = "smsPartTotal";  
 
       public static final String ACTION_SMS_SENT = "SENT_SMS_ACTION";  
       public static final String ACTION_SMS_DELIVERED = "DELIVERED_SMS_ACTION";  
 
       public static final String EVENT_ONSMSSENT = "onSmsSent";  
       public static final String EVENT_ONSMSDELIVERED = "onSmsDelivered";  
 
       private final Context mContext;  
       private final Browser mBrowser;  
 
       private String mId = "";  
       private int mTotalSize = 0;  
       private int mSentSize = 0;  
 
       private String getResultString(int resultCode) {  
               switch (resultCode) {  
                       case Activity.RESULT_OK :  
                               return "OK";  
                       case SmsManager.RESULT_ERROR_GENERIC_FAILURE :  
                               return "RESULT_ERROR_GENERIC_FAILURE";  
                       case SmsManager.RESULT_ERROR_NO_SERVICE :  
                               return "RESULT_ERROR_NO_SERVICE";  
                       case SmsManager.RESULT_ERROR_NULL_PDU :  
                               return "RESULT_ERROR_NULL_PDU";  
                       case SmsManager.RESULT_ERROR_RADIO_OFF :  
                               return "RESULT_ERROR_RADIO_OFF";  
                       default :  
                               return "OK";  
               }  
       }  
       private final BroadcastReceiver mSentReceiver = new BroadcastReceiver() {  
               @Override  
               public void onReceive(Context context, Intent intent) {  
                       mSentSize++;  
                       if (mSentSize == mTotalSize) {  
                               context.unregisterReceiver(this);  
                               mBrowser.eventList.run(EVENT_ONSMSSENT, new String[]{mId, getResultString(getResultCode())});  
                       }  
               }  
       };  
       private final BroadcastReceiver mDeliveredReceiver = new BroadcastReceiver() {  
               @Override  
               public void onReceive(Context context, Intent intent) {  
                       context.unregisterReceiver(this);  
                       mBrowser.eventList.run(EVENT_ONSMSDELIVERED, new String[]{mId, intent.getStringExtra("pdu")});  
               }  
       };  
 
       public RexseeSMSSender(Browser browser) {  
               mBrowser = browser;  
               mContext = browser.getContext();  
               browser.eventList.add(EVENT_ONSMSSENT);  
               browser.eventList.add(EVENT_ONSMSDELIVERED);  
       }  
 
       //JavaScript Interface  
       public void send(String phoneNumber, String message) {  
               send(null, phoneNumber, message);  
       }  
       public void send(String id, String phoneNumber, String message) {  
 
               SmsManager sms = SmsManager.getDefault();  
               ArrayList<String> msgs = sms.divideMessage(message);  
               mTotalSize = msgs.size();  
               mSentSize = 0;  
               mId = (id != null && !id.trim().equals("")) ? id : null;  
 
               try {  
                       mContext.unregisterReceiver(mSentReceiver);  
                       mContext.unregisterReceiver(mDeliveredReceiver);  
               } catch (Exception e) {  
               }  
 
               ArrayList<PendingIntent> sentIntents = null;  
               ArrayList<PendingIntent> deliveryIntents = null;  
               if (mId != null) {  
                       sentIntents = new ArrayList<PendingIntent>();  
                       deliveryIntents = new ArrayList<PendingIntent>();  
                       PendingIntent deliverPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0);  
                       for (int i = 0; i < mTotalSize; i++) {  
                               PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_SENT + "_" + i), 0);  
                               sentIntents.add(sentPI);  
                               mContext.registerReceiver(mSentReceiver, new IntentFilter(ACTION_SMS_SENT + "_" + i));  
                               deliveryIntents.add(deliverPI);  
                       }  
                       mContext.registerReceiver(mDeliveredReceiver, new IntentFilter(ACTION_SMS_DELIVERED));  
               }  
               sms.sendMultipartTextMessage(phoneNumber, null, msgs, sentIntents, deliveryIntents);  
 
       }  
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值