fix(MOB): 다운로드 완료 감지를 BroadcastReceiver에서 폴링 방식으로 변경
삼성폰에서 BroadcastReceiver가 동작하지 않는 문제 해결 - BroadcastReceiver 제거, Handler로 1초 간격 DownloadManager.Query 폴링 - 모든 기기에서 안정적으로 다운로드 완료 감지 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,15 +3,15 @@ package com.codebridgex.webapp;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -144,8 +144,8 @@ public class AppUpdateChecker {
|
||||
DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
long downloadId = dm.enqueue(request);
|
||||
|
||||
// 다운로드 완료 감지
|
||||
registerDownloadReceiver(downloadId, fileName);
|
||||
// 다운로드 완료 폴링 감지
|
||||
pollDownloadStatus(downloadId, fileName);
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Download start error", e);
|
||||
@@ -153,32 +153,42 @@ public class AppUpdateChecker {
|
||||
}
|
||||
|
||||
/**
|
||||
* 다운로드 완료 시 설치 화면 표시
|
||||
* 다운로드 상태를 1초마다 폴링하여 완료 감지
|
||||
*/
|
||||
private void registerDownloadReceiver(long downloadId, String fileName) {
|
||||
BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||
private void pollDownloadStatus(long downloadId, String fileName) {
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
||||
if (id != downloadId) return;
|
||||
public void run() {
|
||||
if (activity.isFinishing() || activity.isDestroyed()) return;
|
||||
|
||||
try {
|
||||
activity.unregisterReceiver(this);
|
||||
} catch (Exception ignored) {}
|
||||
DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(downloadId);
|
||||
|
||||
installApk(fileName);
|
||||
try (Cursor cursor = dm.query(query)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
|
||||
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
Log.d(TAG, "Download complete: " + fileName);
|
||||
installApk(fileName);
|
||||
return;
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON));
|
||||
Log.e(TAG, "Download failed, reason: " + reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Poll download status error", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// 아직 다운로드 중 → 1초 후 재확인
|
||||
handler.postDelayed(this, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
// DownloadManager 브로드캐스트는 시스템에서 발송하므로 RECEIVER_EXPORTED 필요
|
||||
activity.registerReceiver(receiver,
|
||||
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
|
||||
Context.RECEIVER_EXPORTED);
|
||||
} else {
|
||||
activity.registerReceiver(receiver,
|
||||
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user