1

我拥有的是多个按钮。使用 if-else 语句,我将文件下载到相应的按钮。现在,我还在 if-else 语句中定义了要通过意图打开的类。我需要它,以便它开始下载文件,然后开始新的活动。我曾经使用 AsyncTask 执行此操作,并在 onPostExecute 中启动新意图,但我认为使用 DownloadManager 会更好。所以,你可能会感到困惑。所以我会通过我的代码来解释......

所以,我在这里设置它:

 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));

好的。现在,在我的 if-else 中,我声明要下载的 url,并设置一个字符串等于一个类,另一个字符串等于输出文件:

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);
            }

好的。所以一切都很好。现在,我的节目下载:

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

好的。现在,它下载了。所以,现在它正在下载,我需要开始一个新的活动。而且,我已经研究并尝试了一些东西,但没有任何效果。如您所见,我已经在字符串中设置了一个类。我有我在 onPostExecute 中使用的这段代码,所以我知道它工作正常:

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

所以,我会重复我想要的。我想下载一个文件,然后在执行下载后,开始一个新的活动。任何帮助是极大的赞赏。谢谢!

编辑 - 这是一个更新的代码:

    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();
          }
}
4

1 回答 1

1

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

于 2013-03-28T02:27:03.877 回答