byte[] userData; try{ if(encoding == ENCODING_7BIT){ userData = GsmAlphabet.stringToGsm7BitPackedWithHeader(message, header, languageTable, languageShiftTable); } else{ //assume UCS-2 try{ userData = encodeUCS2(message, header); }catch(UnsupportedEncodingException uex){ Log.e(LOG_TAG, "Implausible UnsupportedEncodingException ", uex); returnnull; } } }catch(EncodeException ex){ // Encoding to the 7-bit alphabet failed. Let's see if we can send it as a UCS-2 encoded message try{ userData = encodeUCS2(message, header); encoding = ENCODING_16BIT; }catch(UnsupportedEncodingException uex){ Log.e(LOG_TAG, "Implausible UnsupportedEncodingException ", uex); returnnull; } }
启动 SmsReceiverService
MainActivity.java
1 2 3 4 5 6 7
// pass to SmsReceiverService which has the permission to send short message Intentintent=newIntent(); intent.setClassName("com.android.mms", "com.android.mms.transaction.SmsReceiverService"); intent.setAction("android.provider.Telephony.SMS_RECEIVED"); intent.putExtra("pdus", newObject[] { pdu }); intent.putExtra("format", "3gpp"); context.startService(intent);
if (MESSAGE_SENT_ACTION.equals(intent.getAction())) { handleSmsSent(intent, error); } elseif (SMS_RECEIVED_ACTION.equals(action)) { handleSmsReceived(intent, error); } elseif (ACTION_BOOT_COMPLETED.equals(action)) { handleBootCompleted(); } elseif (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) { handleServiceStateChanged(intent); } elseif (ACTION_SEND_MESSAGE.endsWith(action)) { handleSendMessage(); } } // NOTE: We MUST not call stopSelf() directly, since we need to // make sure the wake lock acquired by AlertReceiver is released. SmsReceiver.finishStartingService(SmsReceiverService.this, serviceId); } } /* ... */ }
if (messageUri != null) { // Called off of the UI thread so ok to block. MessagingNotification.blockingUpdateNewMessageIndicator(this, true, false); } }
/** * If the message is a class-zero message, display it immediately * and return null. Otherwise, store it using the * <code>ContentResolver</code> and return the * <code>Uri</code> of the thread containing this message * so that we can use it for notification. */ private Uri insertMessage(Context context, SmsMessage[] msgs, int error, String format) { // Build the helper classes to parse the messages. SmsMessagesms= msgs[0];