【发布时间】:2015-04-13 03:49:56
【问题描述】:
我已经让我的应用程序从网站下载内容,但意图要求您在浏览器中打开链接并从那里下载。我希望改变这一点,所以当我点击下载按钮时,它不会直接指向浏览器,而是会在我的应用中打开一个弹出窗口并显示下载进度。
我怎样才能做到这一点? 我搜索了stackoverflow,只找到了对我不起作用的方法。这是我的代码-
txtLink = (EditText) findViewById(R.id.input_package);
btnOpenLink = (ImageButton) findViewById(R.id.download);
btnOpenLink.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String page = txtLink.getText().toString();
if (!TextUtils.isEmpty(page)) {
Uri uri = Uri.parse(defaultLink + page);
// Success Toast
LayoutInflater inflater = getLayoutInflater();
// Inflate the Layout
View layout = inflater.inflate(R.layout.custom_toast, null);
TextView text = (TextView) layout.findViewById(R.id.textToShow);
// Set the Text to show in TextView
text.setText("Your download should start shortly");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else {
// Fail Toast
LayoutInflater inflater = getLayoutInflater();
// Inflate the Layout
View layout = inflater.inflate(R.layout.custom_toast, null);
TextView text = (TextView) layout.findViewById(R.id.textToShow);
// Set the Text to show in TextView
text.setText("Please enter a package name");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
});
有人能解释一下创建弹出下载进度的最佳方法是什么吗? 因为我有一个输入字段,可以在其中输入 url 的第二部分并下载它。例如,download.com/ 但用户输入“下载”,因此当他点击按钮时,它会更改为 download.com/download 并从该网址下载。
谢谢
【问题讨论】:
-
1.你总是可以使用 DownloadManager(特别是对于大文件) 2. 如果没有,那么任何 http 客户端(Apache、URLConnection 等) 3. 对于每个 http 客户端,都有很多示例如何通过 Internet 执行此操作......所以在其他话:做更多的研究
-
您应该在一个新的
Activity中创建一个AlertDialog并打开该下载链接的onClick活动,不要忘记在AlertDialog的积极按钮上完成这几行987654326@ -
感谢您的回复,我会对此进行更多研究。
标签: java android android-intent download android-download-manager