- com.android.mms.transaction.PrivilegedSmsReceiver
- com.android.mms.transaction.SmsReceiver
- com.android.mms.transaction.SmsReceiverService
- com.android.mms.transaction.MessagingNotification
二、圖解
注意:SeviceHandler是SmsReceiverService的內部類,SmsReceiver是PrivlegedSmsReceiver的父類;
三、詳細分析
3.1 PrivilegedSmsReceiver到SmsReceiverService
1)
PrivilegedSmsReceiver這個接收器從中間才能獲取數據
PrivilegedSmsReceiver是一個廣播接收器而且繼承自SmsReceiver,在AndroidManifest.xml 中有以下聲明:
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
android.provider.Telephony.SMS_RECEIVED該action在那被使用到了?若是你們有看過度析中間層的接收流程的童鞋就很清楚了,中間層處理接收到的短信的時侯最後會調用到SMSDispatcher的protected void dispatchPdus(byte[][] pdus) 方法,讓咱們回眸一下:
- protected void dispatchPdus(byte[][] pdus) {
- Intent intent = new Intent(Intents.SMS_RECEIVED_ACTION);
- intent.putExtra("pdus", pdus);
- intent.putExtra("encoding", getEncoding());
- intent.putExtra("sub_id", mPhone.getSubscription()); //Subscription information to be passed in an intent
- dispatch(intent, "android.permission.RECEIVE_SMS");
- }
你們確定會問dispatch又幹了些什麼了?
請看下面:
- void dispatch(Intent intent, String permission) {
- mWakeLock.acquire(WAKE_LOCK_TIMEOUT);
- mContext.sendOrderedBroadcast(intent, permission, mResultReceiver,
- this, Activity.RESULT_OK, null, null);
- }
看到這就不用我多說了吧,很顯然是發送了一個叫作Intents.SMS_RECEIVED_ACTION的廣播,那又有人刨根問底兒了,上面兩個值同樣嗎?請看intent中對該變量的定義:
- @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
- public static final String SMS_RECEIVED_ACTION =
- "android.provider.Telephony.SMS_RECEIVED";
到這你們應該明白PrivilegedSmsReceiver會接收到中間層的廣播,而且該廣播很不通常它承載了短信的內容,它從中間層接過接力棒繼續向上傳遞。
2)
PrivilegedSmsReceiver傳遞數據
PrivilegedSmsReceiver從中間層獲取到短信的數據後會調用onReceiveWithPrivilege()方法,該方法定義在它的父類SmsReceiver中。該方法沒有作太多的操做,僅僅是傳遞消息,一下是其核心代碼:
- protected void onReceiveWithPrivilege(Context context, Intent intent, boolean privileged) {
- if (!privileged && (intent.getAction().equals(Intents.SMS_RECEIVED_ACTION)
- || intent.getAction().equals(Intents.SMS_CB_RECEIVED_ACTION))) {
- return;
- }
-
- intent.setClass(context, SmsReceiverService.class);
- intent.putExtra("result", getResultCode());
- beginStartingService(context, intent);
- }
它將處理短信的任務交到SmsReceiverService的手中,SmsReceiverService纔是真正幹活的傢伙。
3)SmsReceiverService處理
SmsReceiverService它是一個服務,當它開啓的時候:首先是在onCreate中初始化,其中初始化最重要的工做就是實例化ServiceHandler對象,ServiceHandler該類是SmsReceiverService的一個內部類,繼承自Handler,如下是它的定義代碼:
- private final class ServiceHandler extends Handler {
- public ServiceHandler(Looper looper) {
- super(looper);
- }
- /**
- * Handle incoming transaction requests.
- * The incoming requests are initiated by the MMSC Server or by the MMS Client itself.
- */
- @Override
- public void handleMessage(Message msg) {
- int serviceId = msg.arg1;
- Intent intent = (Intent)msg.obj;
- if (intent != null) {
- String action = intent.getAction();
-
- int error = intent.getIntExtra("errorCode", 0);
-
- if (MESSAGE_SENT_ACTION.equals(intent.getAction())) {
- handleSmsSent(intent, error);
- } else if (SMS_RECEIVED_ACTION.equals(action)) {
- handleSmsReceived(intent, error);
- } else if (SMS_CB_RECEIVED_ACTION.equals(action)) {
- handleCbSmsReceived(intent, error);
- } else if (ACTION_BOOT_COMPLETED.equals(action)) {
- handleBootCompleted();
- } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
- handleServiceStateChanged(intent);
- } else if (ACTION_SEND_MESSAGE.endsWith(action)) {
- handleSendMessage(intent);
- }
- }
- // 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);
- }
- }
走到這咱們能夠看出該對象的重要性,便是處理短信真正的苦力,咱們繼續看是怎麼調用到這的。
onCreate走完請看 onStartCommand方法:
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0;
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- msg.obj = intent;
- mServiceHandler.sendMessage(msg);
- return Service.START_NOT_STICKY;
- }
看到嗎,到這它已經順利脫手交給
ServiceHandler對象去異步處理。
4)ServiceHandler處理接收到的短信
根據不一樣的action處理,因爲這裏是短信的接收SMS_RECEIVED_ACTION,因此調用 handleSmsReceived(intent, error)方法,該方法的處理邏輯以下所示:
說明在insertMessage方法時會判斷當前是替換仍是插入,對於替換短信,筆者不是很清楚在什麼狀況下會走這條路。blockingUpdateNewMessageIndicator方法會用notification提醒用戶,而且在方法內會判斷當前用戶是否須要顯示發送報告。
3.2 刷新會話列表
走到上面的代碼,短信已經入庫,但界面的刷新是如何實現的了?
1)會話列表的初始化
ConversationList繼承自ListActivity,用於顯示短信的會話列表,在該類的onStart方法裏有調用了一個重要的方法startAsyncQuery()方法:
- private void startAsyncQuery() {
- try {
- setTitle(getString(R.string.refreshing));
- setProgressBarIndeterminateVisibility(true);
-
- Conversation.startQueryForAll(mQueryHandler, THREAD_LIST_QUERY_TOKEN);
- } catch (SQLiteException e) {
- SqliteWrapper.checkSQLiteException(this, e);
- }
- }
解析:
startQueryForAll方法定義:
- public static void startQueryForAll(AsyncQueryHandler handler, int token) {
- handler.cancelOperation(token);
- handler.startQuery(token, null, sAllThreadsUri,
- ALL_THREADS_PROJECTION, null, null, Conversations.DEFAULT_SORT_ORDER);
- }
這裏會使用mQueryHandler去查詢數據庫,查詢完後會回調該對象的onQueryComplete方法,在該方法裏填充了mListAdapter,使得會話列表得以顯示到界面上。如下代碼是其定義:
- private final class ThreadListQueryHandler extends AsyncQueryHandler {
- public ThreadListQueryHandler(ContentResolver contentResolver) {
- super(contentResolver);
- }
- @Override
- protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
- switch (token) {
- case THREAD_LIST_QUERY_TOKEN:
- mListAdapter.changeCursor(cursor);
- setTitle(mTitle);
- setProgressBarIndeterminateVisibility(false);
-
- if (mNeedToMarkAsSeen) {
- mNeedToMarkAsSeen = false;
- Conversation.markAllConversationsAsSeen(getApplicationContext());
- // Database will be update at this time in some conditions.
- // Wait 1s and ensure update complete.
- mQueryHandler.postDelayed(new Runnable() {
- public void run() {
- // Delete any obsolete threads. Obsolete threads are threads that aren't
- // referenced by at least one message in the pdu or sms tables.
- Conversation.asyncDeleteObsoleteThreads(mQueryHandler,
- DELETE_OBSOLETE_THREADS_TOKEN);
- }
- }, 1000);
- }
- break;
- default:
- Log.e(TAG, "onQueryComplete called with unknown token " + token);
- }
- }
-
- }
這裏爲何要特別提到該對象了,後面更新的操做與它有着密不可分的關係。
mListAdapter該對象是ConversationListAdapter的對象,該對象在ConversationList的oncreate方法裏調用 initListAdapter()進行的初始化。 initListAdapter()對adapter進行初始化:
- private void initListAdapter() {
- mListAdapter = new ConversationListAdapter(this, null);
- mListAdapter.setOnContentChangedListener(mContentChangedListener);
- setListAdapter(mListAdapter);
- getListView().setRecyclerListener(mListAdapter);
- }
mListAdapter.setOnContentChangedListener(mContentChangedListener);是當adapter的內容發生變化,會去執行監聽器的onContentChanged的方法。那爲了弄清楚
mContentChangedListener的定義,查看如下代碼:
- private final ConversationListAdapter.OnContentChangedListener mContentChangedListener =
- new ConversationListAdapter.OnContentChangedListener() {
- public void onContentChanged(ConversationListAdapter adapter) {
- startAsyncQuery();
- }
- };
從新調用startAsyncQuery() 該方法刷新。
2)會話列表的更新
看到上面監聽器所作的工做你們應該明白啦,會話列表的更新靠的就是這個監聽器,當內容發生改變就會從新查詢,界面進行刷新,到此爲止 短信的界面刷新完成。
特別注意:該狀況是用戶在短信會話列表這個界面,若是不在這個界面大概還有其餘兩種狀況: 一、在某個會話中;二、沒有進入mms程序。對於前一種狀況會在下面繼續分析,對於後一種狀況我想也不用多說在這種狀況下會走activity的聲明周期函數,在onstart方法裏進行查詢顯示前面已經提到。那還有一種特殊的狀況就是在從某個會話中返回到會話列表時的處理。下面請看ConversationList的聲明:
- <activity android:name=".ui.ConversationList"
- android:label="@string/app_label"
- android:configChanges="orientation|keyboardHidden"
- android:launchMode="singleTop">
屬性是singleTop,你們都知道這種狀況會去調用onNewIntent方法:
- @Override
- protected void onNewIntent(Intent intent) {
- // Handle intents that occur after the activity has already been created.
- startAsyncQuery();
- }
該方法又會去從新查詢刷新界面。
3.23刷新會話內容
刷新ui除了刷新會話列表以外,還有一種狀況就是當用戶在某個會話時,這時該會話接收到新的消息,這時須要刷新會話的內容,這是怎麼實現的?
用於會話顯示的activity:ComposeMessageActivity;用於顯示會話的短信內容組件: MessageListView;填充listview的adapter是:MessageListAdapter
1)初始化
ComposeMessageActivity的onCreate方法調用initialize方法,initialize方法再調用initMessageList()完成初始化
- private void initMessageList() {
- if (mMsgListAdapter != null) {
- return;
- }
- String highlightString = getIntent().getStringExtra("highlight");
- Pattern highlight = highlightString == null
- ? null
- : Pattern.compile("\\b" + Pattern.quote(highlightString), Pattern.CASE_INSENSITIVE);
- // Initialize the list adapter with a null cursor.
- mMsgListAdapter = new MessageListAdapter(this, null, mMsgListView, true, highlight);
- mMsgListAdapter.setOnDataSetChangedListener(mDataSetChangedListener);
- mMsgListAdapter.setMsgListItemHandler(mMessageListItemHandler);
- mMsgListView.setAdapter(mMsgListAdapter);
- mMsgListView.setItemsCanFocus(false);
- mMsgListView.setVisibility(View.VISIBLE);
- mMsgListView.setOnCreateContextMenuListener(mMsgListMenuCreateListener);
- mMsgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if (view != null) {
- ((MessageListItem) view).onMessageListItemClick();
- }
- }
- });
- }
說明:MessageListAdapter定義了一個監聽器當數據發生變化的時候回調監聽器的onContentChanged的方法,該方法會從新查詢該會話相關的內容並刷新顯示,如下是其定義:
- private final MessageListAdapter.OnDataSetChangedListener
- mDataSetChangedListener = new MessageListAdapter.OnDataSetChangedListener() {
- public void onDataSetChanged(MessageListAdapter adapter) {
- mPossiblePendingNotification = true;
- }
- public void onContentChanged(MessageListAdapter adapter) {
- startMsgListQuery();
- }
- };
2)
MessageListAdapter內容的初始化
ComposeMessageActivity的onStart函數裏面調用一個重要的方法loadMessageContent();該方法會繼續調用startMsgListQuery(),在上面的adapter的監聽器裏當內容有變更時回調函數也會調用該方法,如下代碼是該方法作的具體工做:
- private void startMsgListQuery() {
- Uri conversationUri = mConversation.getUri();
- if (conversationUri == null) {
- return;
- }
- if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
- log("for " + conversationUri);
- }
- // Cancel any pending queries
- mBackgroundQueryHandler.cancelOperation(MESSAGE_LIST_QUERY_TOKEN);
- try {
- // Kick off the new query
- mBackgroundQueryHandler.startQuery(
- MESSAGE_LIST_QUERY_TOKEN, null, conversationUri,
- PROJECTION, null, null, null);
- } catch (SQLiteException e) {
- SqliteWrapper.checkSQLiteException(this, e);
- }
- }
分析:該方法所作的工做就是使用mBackgroundQueryHandler查詢數據庫(
mBackgroundQueryHandler是一個AsyncQueryHandler的對象),查詢完成後會回調
mBackgroundQueryHandler該對象的onQueryComplete方法,如下是其核心代碼:
- @Override
- protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
- switch(token) {
- case MESSAGE_LIST_QUERY_TOKEN:
- // Set last sub used in this conversation thread.
- if (cursor.getCount() > 0) {
- cursor.moveToLast();
- mLastSubInConv = cursor.getInt(COLUMN_SUB_ID); //TODO: ADD SUBSCRIPION HERE
- cursor.moveToPosition(-1);
- } else {
- mLastSubInConv = SUBSCRIPTION_ID_INVALID;
- }
- int newSelectionPos = -1;
- long targetMsgId = getIntent().getLongExtra("select_id", -1);
- if (targetMsgId != -1) {
- cursor.moveToPosition(-1);
- while (cursor.moveToNext()) {
- long msgId = cursor.getLong(COLUMN_ID);
- if (msgId == targetMsgId) {
- newSelectionPos = cursor.getPosition();
- break;
- }
- }
- }
- mMsgListAdapter.changeCursor(cursor);
- if (newSelectionPos != -1) {
- mMsgListView.setSelection(newSelectionPos);
- }
- if (cursor.getCount() == 0 && !isRecipientsEditorVisible() && !mSentMessage) {
- initRecipientsEditor();
- }
- mTextEditor.requestFocus();
- mConversation.blockMarkAsRead(false);
- mConversation.setMessageCount(cursor.getCount());
- return;
-
- }
- }
代碼雖多,但其核心就是對mMsgListAdapter的內容從新賦值刷新界面完畢。
3)刷新
刷新就很簡單啦,當數據有變化的時候會觸發OnDataSetChangedListener這個監聽器,這個監聽器會調用onContentChanged函數從新查詢達到刷新的效果。