处理我的应用程序中的下载意图

时间:2016-07-04 01:47:13

标签: android android-intent

我的webview应用程序已处理外部网址(可以打开来自viber,浏览器等外部应用程序的链接),如下所示 - (i got this code from here

// get URL from browsable intent filter
    TextView uri = (TextView) findViewById(R.id.urlField);
    // get the url
    url = getIntent().getDataString();
    Uri data = getIntent().getData();
    if (data == null) {
        uri.setText("");
    } else {
        uri.setText(getIntent().getData().toString());
        fadeout();
    }
    // }

在我的网页设置

// Load URL from Browsable intent filter if there is a valid URL pasted
    if (uri.length() > 0)
        webView.loadUrl(url);
    else
        // dont load

这就是我在webview中处理下载的方式

// download manager
    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimeType,
                long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                    mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();
        }
    });
    // download manager

但是,我想处理其他应用程序传递的下载意图。我该怎么做?

这是我用来从另一个应用程序打开系统默认应用程序选择器。(我的应用程序在这里列出) -

   // download via...
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
            Toast.makeText(getApplicationContext(), "don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",
                    Toast.LENGTH_SHORT).show();
        }
    });
    // download via..

每当我点击下载链接时,我的应用程序都会打开,但只是静坐而不是打开网址。我希望它像下载应用程序一样

2 个答案:

答案 0 :(得分:3)

你必须解决问题

  • (a)始终显示选择器
  • (b)告诉android你的应用应该成为选择者的一部分
  • (c)处理“下载”

(a)始终显示选择器

替换

startActivity(Intent.createChooser(i, "caption of the choser"));

<manifest ... >
    <application ... >
        <activity android:name=".MyDownloadActivity" ...>
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:mimeType="*/*"
                    android:scheme="file" />
            </intent-filter>
            <intent-filter > <!-- mime only SEND(_TO) -->
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.CATEGORY_OPENABLE" />

                <!--
                these do not work :-(
                <data android:scheme="content" android:host="*"  />
                <data android:scheme="content" 
                    android:host="media" 
                    android:pathPattern="/external/images/media/.*" />
                <data android:host="*" android:scheme="content" 
                    android:mimeType="image/*" />
                -->
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>
    </application>

</manifest>

(b)告诉android你的应用应该成为选择器的一部分

在清单中,您必须声明您有一个可以处理相关内容的活动

MyDownloadActivity.onCreate()

*(c)处理“下载”

MyDownloadActivity 您的{{1}}将收到SEND,SENDTO,VIEW的所有mime-types * / *。有关详细信息,请参阅@ Vickyexpert的回答

intent-intercept是一个免费的Android工具来分析意图。它几乎可以拦截每一个意图。您可以查看源代码以了解如何完成此操作。

我使用sendtosd作为我的通用下载处理程序。它的源代码编码器可以在github上找到

答案 1 :(得分:2)

您需要将以下代码放在您的活动中,以响应意图,

void onCreate(Bundle savedInstanceState) {
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_VIEW.equals(action))
    {
        */if (type.startsWith("image/“))
          {*/
            downloadImage(intent); // Download image being sent
        }
    }

void downloadImage(Intent intent) {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
        // Write Your Download Code Here
    }
}