简体   繁体   中英

Android - Start Intent after download using DownloadManager

What I have is multiple buttons. Using if-else statements, I download a file to that corresponding button. Now, I also define what class to open via intent in the if-else statement. I need to have it so that it will begin downloading the file and then start a new activity. I used to do this with an AsyncTask, and start the new intent in the onPostExecute, but I decided it's better to use DownloadManager. So, you may be confused. So I'll explain through my code...

So, here I'm setting it all up:

 BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Ok. Now, in my if-else I declare the url to download, as well as setting a string equal to a class, and another string equal to the output file:

if (andy != null){
                className = "com.cydeon.plasmamodz.Softkeys";
                fileName = "batterymod.zip";
                dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                Request req = new Request(
                        Uri.parse("https://dl.dropbox.com/s/gfukrwqy4xqrnj9/Android.zip"));
                req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                        fileName);
                enqueue = dm.enqueue(req);
            }

Ok. So all works good. Now, my showDownload:

public void showDownload(View view) {
    Intent i = new Intent();
    i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
    startActivity(i);

Good. Now, it downloads. So, now that's it's downloading, I need to start a new activity. And, I've researched and tried some stuff, but nothing works. As you saw, I already set a class inside the string. I have this code which I used in the onPostExecute, so I know it works fine:

        try {
          Intent openNewIntent = new Intent(Bmod.this, Class.forName(className) );
          startActivity( openNewIntent );
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
          }
        }

So, I'll repeat what I want. I want to download a file, and then, after executing the download, start a new activity. Any help is greatly appreciated. Thanks!

Edit - Here's an updated code:

    public void showDownload(View view) {
    Context context = getApplicationContext();
    CharSequence text = "Download complete";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    try {
          Intent openNewIntent = new Intent(Bmod.this, Class.forName(className) );
          startActivity( openNewIntent );
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
          }
}

将startActivity()调用放入广播接收器的onReceive()中,以便在通知接收器下载完成时开始活动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM