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)。