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);  
 
       }  
 
}

僅對Rexsee的源碼和函數事件作了整理,相關的demo或源碼解析能夠在Rexsee社區瞭解,目前Rexsee已提供了近2000個擴展,覆蓋Android原生功能超過90%,且所有開放: http://www.rexsee.com/
相關文章
相關標籤/搜索