Virendra Varma
Virendra Varma

Reputation: 935

Broadcast receiver IntentReceiverLeaked

I am trying to download a file and after download want to update it in the database, but in my intent the service broadcast receiver is creating this issue:

android.app.IntentReceiverLeaked: Service com.dev.newtermain.services.intent_service.DownloadService has leaked IntentReceiver com.dev.newtermain.services.intent_service.DownloadService$DownloadCompleteReceiver@d4fdde9 that was originally registered here. Are you missing a call to unregisterReceiver()?
    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:731)
    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1179)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1159)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1153)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
    at com.dev.newtermain.services.intent_service.DownloadService.onStartCommand(DownloadService.java:70)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3228)
    at android.app.ActivityThread.-wrap17(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1591)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5769)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Here is my intent service code:

public class DownloadService extends IntentService {
 public DownloadService() {
    super("Download Service");
}

Long downloadId;
private DownloadManager downloadManager;

@Override public void onCreate() {
    super.onCreate();
    downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    completeReceiver = new DownloadCompleteReceiver();
}

@Override public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    onHandleIntent(intent);
    this.intent = intent;
    return START_STICKY;
}

@Override
protected void onHandleIntent(Intent intent) {

    String key = intent.getStringExtra(DOWNLOAD_KEY);
    messageId = intent.getStringExtra(MESSAGE_ID);
    mReactionId = intent.getStringExtra(REACTION_ID);
    int messageType = intent.getIntExtra(MESSAGE_TYPE, 3);
    initDownload(key, messageType);
}

private void initDownload(String key, int messageType) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.addRequestHeader("Authorization", SharedPref.User.getToken());

    // In order for this if to run, you must use the Android 3.2 to
    // compile your app
    request.allowScanningByMediaScanner();
    request.setAllowedOverMetered(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(path, fileName);

    // Get download service and enqueue file
    downloadId = downloadManager.enqueue(request);

    Logs.e("file download", "downloading begin");
    IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    this.registerReceiver(completeReceiver, intentFilter);
}

public class DownloadCompleteReceiver extends BroadcastReceiver {
    public DownloadCompleteReceiver() {
    }

    @Override public void onReceive(Context context, Intent intent) {
        Logs.e("file download", "broadcast received");
        DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
        Cursor cursor = downloadManager.query(query);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            int status = cursor.getInt(columnIndex);
            cursor.close();

            switch (status) {
                case DownloadManager.STATUS_SUCCESSFUL:
                    Logs.e("file download", "download successful");
                    updateDb(fileName, messageId, Id);
                    break;
                case DownloadManager.STATUS_FAILED:
                    Logs.e("file download", "download Fail");
                    break;
                case DownloadManager.STATUS_PENDING:
                    Logs.e("file download", "STATUS_PENDING");
                    break;
                case DownloadManager.STATUS_RUNNING:
                    Logs.e("file download", "STATUS_RUNNING");
                    break;
            }
        }
    }
}

@Override public void onDestroy() {
    Logs.e("file download", "stopping service");
    this.unregisterReceiver(completeReceiver);
    downloadManager.remove(downloadId);
    super.onDestroy();
}

As the documentation of the broadcast receiver suggests, I am unregistering the broadcast receiver, but I am not getting why I am getting this issue.

EDIT

I Applied all suggest answers the new Problem is If I add a broadcast receiver it will give success response but not saving file but when I remove the broadcast receiver file download successfully

Upvotes: 1

Views: 937

Answers (2)

Dhaval Solanki
Dhaval Solanki

Reputation: 4705

Just replace your code with changing the position of your broadcast unregister function

@Override public void onDestroy() {
    downloadManager.remove(downloadId);
    this.unregisterReceiver(completeReceiver);
    super.onDestroy();
}

Upvotes: 1

Sagar
Sagar

Reputation: 24907

Problem:

The reason you are facing this issue is because IntentService runs to completion before the download is completed and your DownloadCompleteReceiver is still registered.

downloadId = downloadManager.enqueue(request); doesn't download in a synchronous manner. As a result the IntentService runs to completion.

Solution Approach

If it's mandatory for you to have the status received, you can start a ForegroundService instead of IntentService and then register the receiver. In onDestory() of your Service, unregister the receiver.

Upvotes: 1

Related Questions