2

I am getting a DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast upon getting I am required to extract the zip file and fill the the data base I have written the code to do my work .But I have to start the Thread here to handle the file extraction and insertion to database.

I know that a thread cannot be started inside broadcast receiver . If any body has encounter the problem please suggest the solution .. as my work will take time in insertion so i dont want to wait and stick with it.

I am getting exception while running thread inside broadcast receiver and

Writing this code also not working inside broadcast receiver

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {

        /*
         * check status message whether the last queue is successful and
         * completed and then stop self
         */
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
            Intent mIntent = new Intent(downloadmanager1.this, DBService.class);
            mIntent.putExtra("messenger", new Messenger(mHandler));
            mIntent.putExtra("enqueue", enqueue);
            ctxt.startService(mIntent);
          //  ctxt.startService(mIntent);
        }
    }

Note - no exception or error is through and neither any Intentservice is started in DDMS while debugging
};

Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29

1 Answers1

5

Step #1: Write an IntentService that does the work to "to handle the file extraction and insertion to database".

Step #2: Call startService() from onReceive() of your BroadcastReceiver to pass control to the IntentService.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @CommonsWare why thread inside broadcast receiver does not worked? i was getting a same issue in one of my project. please explain! – Hardik Feb 26 '16 at 08:28
  • @Hardik because a broadcast receiver will most likely be killed by android after the onReceive() is done. – Juan Acevedo Jun 15 '16 at 03:28