i am new to download manager side of android, i can't cancel the download when user clicked a button,i am putting the current id of request but still havent solve it, help me thnks in advance heres the main 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)
create a new task and return a new id.You should use the previous 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();
}