투케이2K

320. (AndroidStudio/android/java) DownloadManager 다운로드 매니저 사용해 파일 다운 및 응답 결과 콜백 확인 본문

Android

320. (AndroidStudio/android/java) DownloadManager 다운로드 매니저 사용해 파일 다운 및 응답 결과 콜백 확인

투케이2K 2022. 8. 12. 13:31
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : java

 

[다운로드 매니저 사용하는 쪽]

// TODO [DownloadManager 사용해 파일 다운로드 수행 실시]
DownloadManager manager = (DownloadManager) mContext.getSystemService(Activity.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url.trim()); // [파일 다운로드 주소 : 확장자명 포함되어야함]
DownloadManager.Request request = new DownloadManager.Request(uri); // [다운로드 매니저 객체 생성 및 주소 지정 실시]
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // [앱 상단에 다운로드 표시]
request.setDescription("Download ... "); // [다운로드 중 표시되는 내용]
request.setNotificationVisibility(1); // [앱 상단에 다운로드 상태 표시]
request.setTitle(fileName); // [다운로드 제목 표시]
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); // [다운로드 폴더 지정]
long downloadID = manager.enqueue(request); // [다운로드 수행 및 결과 값 지정]


// TODO [다운로드 결과 브로드 캐스트 알림 전달]
Intent intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
intent.putExtra("ACTION_DOWNLOAD_COMPLETE", downloadID);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
 

[응답 결과 콜백 확인하는 쪽]

// TODO [onCreate 부분에서 브로드캐스트 알림 등록]
IntentFilter filter = new IntentFilter();
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); // 파일 다운로드 완료 알림 받음
LocalBroadcastManager.getInstance(A_Main.this).registerReceiver(mMessageReceiver, filter);




// TODO [onDestroy 부분에서 브로드캐스트 알림 해제]
LocalBroadcastManager.getInstance(A_Main.this).unregisterReceiver(mMessageReceiver);




// TODO [실시간 이벤트 감지 브로드캐스트 이벤트 리스너]
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("---","---");
            Log.w("//===========//","================================================");
            Log.i("","\n"+"[A_Main >> onReceive() :: "+String.valueOf(intent.getAction())+"]");
            Log.w("//===========//","================================================");
            Log.i("---","---");

            // ------------------------------------------
            if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { // [파일 다운로드 상태 확인]

                long downState = intent.getLongExtra("ACTION_DOWNLOAD_COMPLETE", 0); // [인텐트로 넘어온 데이터 확인]

                Log.i("---","---");
                Log.w("//===========//","================================================");
                Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 결과 확인 실시]");
                Log.i("","\n"+"[downState :: "+String.valueOf(downState)+"]");
                Log.w("//===========//","================================================");
                Log.i("---","---");

                try {
                    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

                    Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downState));

                    if (cursor != null && cursor.moveToNext()) {
                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        cursor.close();

                        // -----------------------------
                        // [파일 다운로드 상태 확인 실시]
                        // -----------------------------

                        if (status == DownloadManager.STATUS_FAILED) {
                            Log.i("---","---");
                            Log.e("//===========//","================================================");
                            Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 진행 결과]");
                            Log.i("","\n"+"[state :: "+String.valueOf("DownloadManager.STATUS_FAILED")+"]");
                            Log.e("//===========//","================================================");
                            Log.i("---","---");
                        }
                        else if (status == DownloadManager.STATUS_PAUSED) {
                            Log.i("---","---");
                            Log.d("//===========//","================================================");
                            Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 진행 결과]");
                            Log.i("","\n"+"[state :: "+String.valueOf("DownloadManager.STATUS_PAUSED")+"]");
                            Log.d("//===========//","================================================");
                            Log.i("---","---");
                        }
                        else if (status == DownloadManager.STATUS_PENDING) {
                            Log.i("---","---");
                            Log.d("//===========//","================================================");
                            Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 진행 결과]");
                            Log.i("","\n"+"[state :: "+String.valueOf("DownloadManager.STATUS_PENDING")+"]");
                            Log.d("//===========//","================================================");
                            Log.i("---","---");
                        }
                        else if (status == DownloadManager.STATUS_RUNNING) {
                            Log.i("---","---");
                            Log.d("//===========//","================================================");
                            Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 진행 결과]");
                            Log.i("","\n"+"[state :: "+String.valueOf("DownloadManager.STATUS_RUNNING")+"]");
                            Log.d("//===========//","================================================");
                            Log.i("---","---");
                        }
                        else if (status == DownloadManager.STATUS_SUCCESSFUL) {
                            Log.i("---","---");
                            Log.w("//===========//","================================================");
                            Log.i("","\n"+"[A_Main >> onReceive() :: 파일 다운로드 진행 결과]");
                            Log.i("","\n"+"[state :: "+String.valueOf("DownloadManager.STATUS_SUCCESSFUL")+"]");
                            Log.w("//===========//","================================================");
                            Log.i("---","---");
                        }
                    }
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
            // ------------------------------------------
        }
    };

 

반응형
Comments