WEB开发网
开发学院手机开发Android 开发 Android通过AIDL实现下载进程通信 阅读

Android通过AIDL实现下载进程通信

 2012-11-08 19:04:01 来源:WEB开发网   
核心提示:percent = nowPercent;updateProgressBar(queue.getId(), UPDATE_NOTIFY_PROGRESS,nowPercent, notify);}//如果取消则停止下载if(queueList.get(queue.getId()).isCancel()){nManage
percent = nowPercent;
updateProgressBar(queue.getId(), UPDATE_NOTIFY_PROGRESS,
nowPercent, notify);
}

//如果取消则停止下载
if(queueList.get(queue.getId()).isCancel()){
nManager.cancel(queue.getId());
file.delete();
break;
}

} while (true);

//如果取消则停止下载,否则保存文件,并更新notify到完成状态
if (!queueList.get(queue.getId()).isCancel()) {
output.flush();
if (queue.getCompleteIntent() != null && !("").equals(queue.getCompleteIntent())) {
notify.contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0,
new Intent(queue.getCompleteIntent()),
PendingIntent.FLAG_UPDATE_CURRENT);
}
updateProgressBar(queue.getId(), UPDATE_NOTIFY_COMPLETE,
100, notify);
}else{
nManager.cancel(queue.getId());
}

} catch (Exception e) {
//更新notify状态到异常
updateProgressBar(queue.getId(), DOWNLOAD_EXCEPTION_NOTIFY,
100, notify);
queueList.get(queue.getId()).setCancel(true);
Log.e(TAG, e.toString(), e);
e.printStackTrace();
} finally {
try {
output.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//是否完成后自动运行completeIntent
if(!queueList.get(queue.getId()).isCancel() && queueList.get(queue.getId()).isAutoRun()){
if (queue.getCompleteIntent() != null && !("").equals(queue.getCompleteIntent())) {
getApplicationContext().startActivity(new Intent(queue.getCompleteIntent()));
}
}
//队列中移除
queueList.remove(queue.getId());
}

/**
* 更新队列
* @param int notifyId 需要更新的notify id
* @param int what handler更新标识
* @param int progress 进度条刻度
* @param Notification notify notify实例
**/
private void updateProgressBar(int notifyId, int what, int progress,
Notification notify) {
//从消息池取一条设定参数的消息
Message msg = Message.obtain(handler, what, progress, notifyId, notify);
handler.sendMessage(msg);
}

//消息处理,主要更新notify的contentView对象的UI
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
Notification notify = (Notification) msg.obj;
int notifi_id = msg.arg2;
switch (msg.what) {
case UPDATE_NOTIFY_PROGRESS:
Notification notifi = (Notification) msg.obj;
int size = msg.arg1;
notifi.contentView.setProgressBar(R.id.downloadProgress, 100,
size, false);
notifi.contentView.setTextViewText(R.id.percetText, size + "%");
nManager.notify(notifi_id, notifi);
break;
case UPDATE_NOTIFY_COMPLETE:
notify.flags = Notification.FLAG_AUTO_CANCEL;
notify.contentView.setProgressBar(R.id.downloadProgress, 100,
100, false);
notify.contentView.setTextViewText(R.id.percetText, "完成");
nManager.notify(notifi_id, notify);
break;
case DOWNLOAD_EXCEPTION_NOTIFY:

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:Android 通过 AIDL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接