FileDocCategorySizeDatePackage
AndroidSmsService.javaAPI DocAndroid 1.5 API8406Wed May 06 22:42:46 BST 2009com.android.im.service

AndroidSmsService

public class AndroidSmsService extends Object implements com.android.im.engine.SmsService

Fields Summary
private static final String
TAG
private static final String
SMS_STATUS_RECEIVED_ACTION
private static final int
sMaxSmsLength
private android.content.Context
mContext
private SmsReceiver
mSmsReceiver
private android.content.IntentFilter
mIntentFilter
HashMap
mListeners
HashMap
mFailureCallbacks
private static long
sNextMsgId
Constructors Summary
public AndroidSmsService(android.content.Context context)


       
        mContext = context;
        mSmsReceiver = new SmsReceiver();
        mIntentFilter = new IntentFilter(
                Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION);
        mIntentFilter.addDataScheme("sms");
        mListeners = new HashMap<Integer, ListenerList>();
        mFailureCallbacks = new HashMap<Long, SmsSendFailureCallback>();
    
Methods Summary
public voidaddSmsListener(java.lang.String from, int port, SmsListener listener)

        ListenerList l = mListeners.get(port);
        if (l == null) {
            l = new ListenerList(port);
            mListeners.put(port, l);

            // We didn't listen on the port yet, register the receiver with the
            // additional port.
            mIntentFilter.addDataAuthority("*", String.valueOf(port));
            mContext.registerReceiver(mSmsReceiver, mIntentFilter);
        }
        l.addListener(from, listener);
    
private static synchronized longgenMsgId()

         
        return sNextMsgId++;
    
public intgetMaxSmsLength()

        return sMaxSmsLength;
    
private static voidlog(java.lang.String msg)

        Log.d(TAG, "[SmsService]" + msg);
    
public voidremoveSmsListener(SmsListener listener)

        Iterator<ListenerList> iter = mListeners.values().iterator();
        while (iter.hasNext()) {
            ListenerList l = iter.next();
            l.removeListener(listener);
            if (l.isEmpty()) {
                iter.remove();
            }
        }
    
public voidsendSms(java.lang.String dest, int port, byte[] data)

        sendSms(dest, port, data, null);
    
public voidsendSms(java.lang.String dest, int port, byte[] data, SmsSendFailureCallback callback)

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            try {
                log(dest + ":" + port + " >>> " + new String(data, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
            }
        }
        if (data.length > sMaxSmsLength) {
            Log.e(TAG, "SMS data message can only contain " + sMaxSmsLength
                    + " bytes");
            return;
        }

        SmsManager smsManager = SmsManager.getDefault();
        PendingIntent sentIntent;
        if (callback == null) {
            sentIntent = null;
        } else {
            long msgId = genMsgId();
            mFailureCallbacks.put(msgId, callback);

            sentIntent = PendingIntent.getBroadcast(mContext, 0,
                new Intent(
                        SMS_STATUS_RECEIVED_ACTION,
                        Uri.parse("content://sms/" + msgId), /*uri*/
                        mContext, SmsReceiver.class),
                0);
        }
        smsManager.sendDataMessage(dest, null/*use the default SMSC*/,
                (short) port, data,
                sentIntent,
                null/*do not require delivery report*/);
    
public voidstop()

        mContext.unregisterReceiver(mSmsReceiver);