AsyncTask
到我的代码下载的PDF文件在后台。
这是我的密码:
public class MainActivity extends AppCompatActivity {
Button btn;
DownloadManager downloadManager;
private String exampleURL = "https://doc.lagout.org/programmation/Actionscript%20-%20Flash%20-%20Flex%20-%20Air/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29.pdf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.download_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(exampleURL);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}
});
}
}
这段代码的另一个问题是,与chrome等应用程序的传统下载不同,您不会收到任何toast消息,告诉您下载已开始,您需要向下拖动小部件以查看是否正在下载。换言之,屏幕下方没有显示下载的迹象。