我是新手下载android的经理方面,我无法在用户点击按钮时取消下载,我正在提出当前的请求ID,但仍然无法解决,请提前帮助我 继承主要课程
public class DownloadManagerActivity extends Activity {
private long enqueue; private DownloadManager dm; private long downloadReference; Query query; Cursor c; String action;
long downloadId;
Request request;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
query = new Query();
query.setFilterById(enqueue);
c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
//
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
Toast.makeText(DownloadManagerActivity.this, "Download Complete", Toast.LENGTH_LONG).show();
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
request = new Request(Uri.parse("http://192.168.10.6/mytv/mashup.mp4"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
public void cancelDownload(View view) {
//downloadReference= intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
downloadReference = dm.enqueue(request);
dm.remove(downloadReference);
Toast.makeText(DownloadManagerActivity.this, "Download Cancelled", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:3)
downloadReference = dm.enqueue(request)
创建一个新任务并返回一个新的id。你应该使用之前的id。
public void cancelDownload(View view) {
//downloadReference= intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
//downloadReference = dm.enqueue(request);
dm.remove(enqueue );
Toast.makeText(DownloadManagerActivity.this, "Download Cancelled", Toast.LENGTH_SHORT).show();
}