Android从链接下载文件

时间:2016-02-16 18:26:34

标签: android android-download-manager download-manager

我已经为Android应用实施了一个解决方案,可以发布到网络服务器并验证Google订单,然后发出下载链接。现在我正在尝试使用App的代码来读取thankyou.php页面中的链接

<a href="http://domain.com/218348214.dat">Download File</a>

该文件是&#34; .DAT&#34;扩展和来自某个链接。应用程序应检索新页面中的链接,并让客户下载该文件。

enter image description here

1 个答案:

答案 0 :(得分:4)

在清单文件中,您需要添加一个意图过滤器:

    <activity
        android:name="Downloader">
        <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:host="domain.com"
                android:scheme="http" />
        </intent-filter>
    </activity>

然后在“下载”活动的onCreate:

public class Download extends Activity {

private String filename;

@Override
protected void onCreate (final Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView(<your layout file>);
    final Intent intent = getIntent ();
    final Uri data = intent.getData ();
    if (data != null) {
        final List<String> pathSegments = data.getPathSegments ();
        fileName = pathSegments.get (pathSegments.size () - 1);
    }
}

然后在下载按钮的clickhandler中,您可以使用视图意图来充当android中的链接。

button.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {
            Uri intentUri = Uri.parse("http://domain.com/" + filename);

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(intentUri);
            startActivity(intent);
        }
    });