簡體   English   中英

Android-使用DownloadManager下載后啟動Intent

[英]Android - Start Intent after download using DownloadManager

我有多個按鈕。 使用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();
          }
}

將startActivity()調用放入廣播接收器的onReceive()中,以便在通知接收器下載完成時開始活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM