SMS注册与接收短信
Android4.2
在Android4.0中,Phone应⽤的Application类PhoneApp的onCreate⽅法中调⽤了
PhoneFactory.makeDefaultPhones(this);
⽽在4.2中此⽅法被移到了PhoneGlobals中,PhoneGlobals类继承⾃ContextWrapper。
在PhoneFactory的makeDefaultPhone⽅法中初始化了RIL类的静态对象sCommandsInterface与Phone的静态对象sProxyPhone。
RIL构造时判断出⼿机⽹络良好就会启动⼀个线程,线程的Runnable对象是RILReceiver的对象。在run⽅法中构建Sockect并向下层发起连接
s = new LocalSocket();
l = new LocalSocketAddress(SOCKET_NAME_RIL,
LocalSocketAddress.Namespace.RESERVED);
如果连接不上每隔4秒重连⼀次。如果连接正常,则获取流,调⽤readRilMessage(is, buffer)⽅法返回除去开头4个字节外能从流中获取的字节数并把获取的字节存于8k⼤⼩限制的buffer中,如果读不到任何字节返回-1。如果返回值为-1,则跳出处理流程,重新进⼊for循环,否则把获取的字节存于Parcel对象中并调⽤processRespon(p)处理获取到的数据。
private void
processRespon (Parcel p) {
int type;
type = p.readInt();
if (type == RESPONSE_UNSOLICITED) {
processUnsolicited (p);
床上三件套} el if (type == RESPONSE_SOLICITED) {
processSolicited (p);
}
releaWakeLockIfDone();
王者妲己
}
接下来读取4个代表类型的字节。⾛if流程processUnsolicited (p)。
接下来再读取4个字节的int数据代表事件类型,接收短信⾛RIL_UNSOL_RESPONSE_NEW_SMS(1003)流程
ca RIL_UNSOL_RESPONSE_NEW_SMS: ret = responString(p); break;
protected Object
responString(Parcel p) {
String respon;
respon = p.readString();
return respon;
}
获取数据中的String后继续处理
ca RIL_UNSOL_RESPONSE_NEW_SMS: {
if (RILJ_LOGD) unsljLog(respon);
// FIXME this should move up a layer
String a[] = new String[2];
a[1] = (String)ret;
SmsMessage sms;
sms = wFromCMT(a);
if (mGsmSmsRegistrant != null) {
早晨说说mGsmSmsRegistrant
.notifyRegistrant(new AsyncResult(null, sms, null));
}
break;
如上:mGsmSmsRegistrant定义在Ril的⽗类中通过下⽅法赋对象
public void tOnNewGsmSms(Handler h, int what, Object obj) {
mGsmSmsRegistrant = new Registrant (h, what, obj);
}
mGsmSmsRegistrant的notifyRegistrant⽅法把得到数据⽤handler传递出去了,接下来跟踪handler是哪个类传过来的。
在构造Phone对象时
sProxyPhone[i] = new YZPhoneProxy(new TDPhone(context,
sCommandsInterface[i], sPhoneNotifier[i]));
可以看出Ril对象赋给了TDPhone对象,TDPhone对象赋给了YZTPhoneProxy对象。查看TDPhone对象的构造发现其调⽤了其⽗类GSMPhone的构造,在GSMPhone构造⽅法中初始化了SMSDispatcher对象
mSMS = new GsmSMSDispatcher(this, mSmsStorageMonitor, mSmsUsageMonitor);
在GsmSMSDispatcher的构造⽅法中调⽤了tOnNewGsmSms⽅法把⾃⼰注册在Ril.java⾥了。
public GsmSMSDispatcher(PhoneBa phone, SmsStorageMonitor storageMonitor,
SmsUsageMonitor usageMonitor) {
super(phone, storageMonitor, usageMonitor);
mDataDownloadHandler = new UsimDataDownloadHandler(mCm);
mCm.tOnNewGsmSms(this, EVENT_NEW_SMS, null);
mCm.tOnSmsStatus(this, EVENT_NEW_SMS_STATUS_REPORT, null);
mCm.tOnNewGsmBroadcastSms(this, EVENT_NEW_BROADCAST_SMS, null);
}
也就是说Ril类⾥接收普通短信的数据最终传到了GsmSMSDispatcher类的handleMessage⽅法⾥,不过GsmSMSDispatcher的handleMessage没处理,⽽是在其⽗类SMSDispatcher的handleMessage⽅法中处理的。
ca EVENT_NEW_SMS:
// A new SMS has been received by the device
if (fal) {
Log.d(TAG, "New SMS Message Received");
}
SmsMessage sms;
ar = (AsyncResult) msg.obj;
if (ar.exception != null) {
Log.e(TAG, "Exception processing incoming SMS. Exception:" + ar.exception);
return;
}
sms = (SmsMessage) ar.result;
try {
int result = dispatchMessage(sms.mWrappedSmsMessage);
if (result != Activity.RESULT_OK) {如何上网
// RESULT_OK means that message was broadcast for app(s) to handle.
// Any other result, we should ack here.
boolean handled = (result == Intents.RESULT_SMS_HANDLED);
notifyAndAcknowledgeLastIncomingSms(handled, result, null);
}
} catch (RuntimeException ex) {
Log.e(TAG, "Exception dispatching message", ex);
notifyAndAcknowledgeLastIncomingSms(fal, Intents.RESULT_SMS_GENERIC_ERROR, null);
}
break;
调⽤了GsmSMSDispatcher中的dispatchMessage(SmsMessageBa smsb)⽅法,在此⽅法中对USIM做了其他处理。判断了系统属性是否允许接收短信,对接收短信的限制可以在这个⽅法⾥做。最后调⽤了SMSDispatcher的dispatchNormalMessage(smsb)⽅法,在此⽅法⼜根据解析数据的来的信息判断流程,最后⾛到dispatchPdus(byte[][] pdus)⽅法,此⽅法发送⼴播
珍珠膏的功效与作用protected void dispatchPdus(byte[][] pdus) {
Intent intent = new Intent(Intents.SMS_RECEIVED_ACTION);
intent.putExtra("pdus", pdus);花枝招展的近义词
intent.putExtra("format", getFormat());
dispatch(intent, RECEIVE_SMS_PERMISSION);
}
下⾯就是应⽤层接收⼴播与处理得到的数据过程了:
接收⼴播的是SmsReceiver的⼦类PrivilegedSmsReceiver,最后⼜把接收到的Intent传递到SmsReceiverService.class中去处理了
} el if (SMS_RECEIVED_ACTION.equals(action)) {
handleSmsReceived(intent, error);
private void handleSmsReceived(Intent intent, int error) {
SmsMessage[] msgs = MessagesFromIntent(intent);
手机黑屏了怎么办
String format = StringExtra("format");
Uri messageUri = inrtMessage(this, msgs, error, format);
if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE) || LogTag.DEBUG_SEND) {
SmsMessage sms = msgs[0];
Log.v(TAG, "handleSmsReceived" + (sms.isReplace() ? "(replace)" : "") +
" messageUri: " + messageUri +
", address: " + OriginatingAddress() +
", body: " + MessageBody());
}
if (messageUri != null) {苏联内战
long threadId = SmsThreadId(this, messageUri);
/
/ Called off of the UI thread so ok to block.
Log.d(TAG, "handleSmsReceived messageUri: " + messageUri + " threadId: " + threadId); MessagingNotification.blockingUpdateNewMessageIndicator(this, threadId, fal);
}
}
handleSmsReceived⽅法把收到的数据存到数据库⾥并发收到信息的通知。