Android短彩信附件機制,將一些認識寫下來,和大家交流一下,同時也方便自己復習。
用戶可以通過附件按鈕,添加附件。以添加幻燈片為例:
如果點擊幻燈片,會走如下代碼:
ComposeMessageActivity.java
private void editSlideshow() {
// The user wants to edit the slideshow. That requires us to persist the slideshow to
// disk as a PDU in saveAsMms. This code below does that persisting in a background
// task. If the task takes longer than a half second, a progress dialog is displayed.
// Once the PDU persisting is done, another runnable on the UI thread get executed to start
// the SlideshowEditActivity.
getAsyncDialog().runAsync(new Runnable() {
@Override
public void run() {
// This runnable gets run in a background thread.
mTempMmsUri = mWorkingMessage.saveAsMms(false);
}
}, new Runnable() {
@Override
public void run() {
// Once the above background thread is complete, this runnable is run
// on the UI thread.
if (mTempMmsUri == null) {
return;
}
Intent intent = new Intent(ComposeMessageActivity.this,
SlideshowEditActivity.class);
intent.setData(mTempMmsUri);
startActivityForResult(intent, REQUEST_CODE_CREATE_SLIDESHOW);
}
}, R.string.building_slideshow_title);
}
這段代碼比較簡單,總的來說就是先構建一個Uri,用於表示附件的唯一Id,然後這個Uri被傳到附件幻燈片編輯頁面,也就是SlideshowEditActivity.java。重點就是在構建Uri的過程中,做了什麼事情。(關於getAsyncDialog(),並不影響我們分析,讀者可跳過。其實這段代碼是異步執行的,也就是說代碼在主線程中調用了一下,主線程就返回了,也就是常說的不阻塞主線程。這段代碼歸根結底是調用了AsycnTask,只不過Mms應用封裝了它並對外提供了異步線程接口。如果第一個Runnable在0.5秒之後沒有執行完畢,那麼會彈出一個progressBar,提示信息就是第三個參數,如果執行完畢,就執行第二個Runnable)。