-5

I am trying to register a broadcastreceiver which listens for "android.intent.action.DOWNLOAD_COMPLETE". But it is not visible in the AndroidManifest.xml. Please help

Kaushal
  • 39
  • 4

1 Answers1

0

You can do like this.

    DownloadManager downloadManager;
    downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("http://xxx.apk");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    //  You can choose network type
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
    downloadManager.getRecommendedMaxBytesOverMobile(getApplicationContext());
    final long flag  = downloadManager.enqueue(request);

    IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE );
    BroadcastReceiver receiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            long anotherFlag = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
            if(anotherFlag == flag){
                // do something you want to do
            }
        }

    };
    registerReceiver(receiver, filter);
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • thanks KeLiuyue, but I was thinking of implementing a class, which implements broadcastreceiver. And for that I was trying to add , which does not show the action name="android.intent.action.DOWNLOAD_COMPLETE". Is it possible? – Kaushal Jul 20 '17 at 06:36
  • @user3678389 yes,you can do that.My way is dynamic registration BroadcastReceive.You're using a static registration BroadcastReceive.It's ok. – KeLiuyue Jul 20 '17 at 07:00
  • @user3678389 you can look at this [https://developer.android.com/guide/components/broadcasts.html](https://developer.android.com/guide/components/broadcasts.html) – KeLiuyue Jul 20 '17 at 07:04