2

我希望能够以通常的方式运行标准的android浏览器,导航到一个带有文件链接的页面,例如

<a href=./myfile.gcsb>click here</a>

单击链接并让它启动我的应用程序,然后它可以根据需要处理文件。

该网页基本上可以正常工作 - 单击 PC 上 Firefox 的链接,正如预期的那样,我可以选择保存文件,或者使用已注册的 .gcsb 文件类型的应用程序打开它。但不是在安卓设备上。

例如,我已经在我的应用程序中尝试了我能想到的每个版本的意图过滤器

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="*" android:mimeType="application/*" android:pathPattern=".*\\.gcsb" />
</intent-filter>

并且它们都不会导致应用程序在单击浏览器中的链接时启动。

我知道有很多关于这个的帖子,但到目前为止,他们都没有帮助我解决这个问题。任何建议都非常感激。(运行 Android 操作系统 2.2)。

4

1 回答 1

2

对其进行排序。首先,从其他帖子看来,在同一窗口中从 android 浏览器打开链接通常存在问题。因此将 target=_blank 添加到原始

<a href ...> ...

处理了这个(虽然见最后的附带条件)。

似乎工作的意图过滤器是

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="*" android:pathPattern=".*\\.gcsb" />
</intent-filter>

并且活动 onCreate() / onRestart() 中的代码是

String  action = intent.getAction();

if (!Intent.ACTION_VIEW.equals(action))
    return;

Uri     uri = intent.getData();
String  scheme = uri.getScheme();
String  name = null;

if (scheme.equals("http")) {
    List<String>
            pathSegments = uri.getPathSegments();

    if (pathSegments.size() > 0)
        name = pathSegments.get(pathSegments.size() - 1);

    URL     url = new URL(uri.toString());
    HttpURLConnection
            urlConnection = (HttpURLConnection)url.openConnection();

    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    is = urlConnection.getInputStream();
}

else if () {
    //  Deal with other schemes (file, content) here
}

//  Should have an open input stream and a target file name/ext by here
//  Make full path to where it needs to be saved from name
//  Open output stream
//  Loop round reading a buffer full of data from input stream and writing to output stream
//  Close everything
//  Not forgetting to deal with errors along the way

这一切似乎都有效,有两个条件:(1)所有这些都应该在后台线程中完成,(2)target=_blank 的效果是让 android 浏览器在每次下载完成时继续打开新窗口- 它打开的数量是有限制的。

于 2013-07-07T09:34:16.260 回答