如何在用户单击 android 时取消下载
how to cancel a download when user clicked in android
我是 android 的下载管理器方面的新手,当用户单击按钮时我无法取消下载,我正在输入请求的当前 ID 但仍然没有解决它,请帮助我进步
这是主要的 class
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();
}
downloadReference = dm.enqueue(request)
创建一个新任务并且 return 一个新的 id.You 应该使用以前的 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();
}
我是 android 的下载管理器方面的新手,当用户单击按钮时我无法取消下载,我正在输入请求的当前 ID 但仍然没有解决它,请帮助我进步 这是主要的 class
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();
}
downloadReference = dm.enqueue(request)
创建一个新任务并且 return 一个新的 id.You 应该使用以前的 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();
}