about : 파일번역 다운로드
메서드로 만들었으나 class로 만드는것이 대체적이다
broadcast는 10초이상 지연되지 않는 간단한 코드로 작성한다
그 이상 넘어갈 시 앱이 죽는 증상 발생
AndroidManifest로 작업할수도 있으나 unregister()를 사용할 수 없는 문제로 사용하지 않았다.(참조링크)
private BroadcastReceiver tFileDownloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID) && intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) != -1 && downloadQueueID != null){ //투표결과와 예외처리
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadQueueID == reference) {
DownloadManager.Query query = new DownloadManager.Query(); // 다운로드 항목 조회에 필요한 정보 포함
query.setFilterById(reference);
Cursor cursor = downloadManager.query(query);
if (!cursor.moveToFirst()) {
return;
}
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int status = cursor.getInt(columnIndex);
int reason = cursor.getInt(columnReason);
cursor.close();
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
if (chatMstVO.getChatDataList() != null) {
int count = rootNode.getChildren().size();
for (int i = count; i > 0; i--) {
TreeNode node = rootNode.getChild(i - 1);
treeView.removeNode(node);
}
for (int i = 0; i < chatMstVO.getChatDataList().size(); i++) {
ChatVO chatVO1 = chatMstVO.getChatDataList().get(i);
TreeNode chatNode = new TreeNode(chatVO1).setViewHolder(new ChatViewHolder(ChatViewActivity.this));
rootNode.addChildren(chatNode);
}
if (scrollView.isScrollEnded) {
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
}
treeView.expandAll();
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(ChatViewActivity.this, getString(R.string.sen077), Toast.LENGTH_SHORT).show();
break;
case DownloadManager.STATUS_PAUSED:
Log.e(TAG, "onReceive: STATUS_PAUSED = " + reason);
break;
case DownloadManager.STATUS_FAILED:
Log.e(TAG, "onReceive: STATUS_FAILED = " + reason);
Toast.makeText(ChatViewActivity.this, getString(R.string.sen104), Toast.LENGTH_SHORT).show();
break;
}
}
}
}
};
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(tFileDownloadReceiver, intentFilter);
if (tFileDownloadReceiver != null) {
try {
unregisterReceiver(tFileDownloadReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}