****** Now a have completed the code, but the notification display is running again after cancel !
I'm using the DownloadManager for downloading a file by url, it works fine.
Code creating Manager:
String[] url = {urlsProz,urlsMb};
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url[i]));
downloadFile=url[i];
request.setDescription("Some description");
request.setTitle("Some titles");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
}
String s= Environment.DIRECTORY_DOWNLOADS;
Log.i("*** testDownloadManager***",s+"/"+url[i]);
request.setDestinationInExternalPublicDir(s, "name-of-the-file"+(++j)+".ext");
// get download service and enqueue file
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//Download progress will be showing in the notification bar
downloadId=manager.enqueue(request);
Log.i("*** testDownloadManager***","downloadId:"+downloadId);
My code after pressing the cancel-Button:
if (manager != null) {
//remove() method will return the number of downloads removed
//Any downloaded files (complete or partial) will be deleted
int i = manager.remove(downloadId);
Log.i(TAG, downloadId + "," + i + " cancelled");
NotificationManager notifManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//notifManager.cancelAll(); //doesn'nt work
manager = null;
}
The manager is cancelled but the notification display is still running even after the app.
Any ideas how to cancel this notification ?
Regards Wicki
cancelAll() method only cancel notification created by your application.
I think you use DownloadManager as this:
// start download
Request request = new Request(Uri.parse(urltoDownload));
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setTitle("my title");
long enqueue = downloadManager.enqueue(request);
So it's DownloadManager app (service) which create notification, and it's only DownloadManager app could cancel it.
You can't cancel other apps notifications, thats not possible.
DownloadManager dm = (DownloadManager) c
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request dlrequest = new DownloadManager.Request(
Uri.parse(url));
dlrequest
.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setTitle("Downloading")
.setDescription("Downloading in Progress..")
.setDestinationInExternalPublicDir("folder_name", name + ".jpg")
**.setNotificationVisibility(visibility)**
.allowScanningByMediaScanner();
dm.enqueue(dlrequest);
.setNotificationVisibility(visibility) -->set visibility true or false. and its done..!
Related
I have implemented DownloadManager in an activity. Download works fine. But this class will automatically create a notification which stays in status bar. Is there a way to remove this notification. Below is the code:
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setDescription(fileName);
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
firmwareZipPath = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ Constants.TEST,
type + ".tgz");
request.setDestinationUri(Uri.fromFile(firmwareZipPath));
downloadId = downloadManager.enqueue(request);
I tried giving false to request.setVisibleInDownloadsUi. Still the notification is shown.
From official doc:
If set to VISIBILITY_HIDDEN, this requires the permission
android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
So you need to add permission to AndroidManifest.xml:
<uses-permission
android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
For more information check official doc
I'm trying to make a download file prosess in my android apps with DownloadManager class and put some Toast with the download status. When I click download button, the Toast always say that my download is pending. Why it happend?
In DownloadManager documnet I'd read that pending mean the download is waiting to start. What is waiting for? Where should I looking for? How do I change the status and start the download?
Here's my code :
public void downloadFile(){
Uri uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"cccc.jpg");
Long ref = downloadManager.enqueue(request);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(ref);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
DownloadStatus(cursor, ref);
}
}
I think my internet is also good no problem with it. The file that I'm trying to get is from my server on 000webhost.com. When I try to get any file from another server the download's start. Is my server that got the problem or my android application?
I wrote this code for you. I hope to help you ❤ :
private DownloadManager downloadManager;
private Uri Download_Uri;
private long refid;
public void downloadFile(){
registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Download_Uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
DownloadManager.Request req = new DownloadManager.Request(Download_Uri);
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
req.setAllowedOverRoaming(false);
req.setTitle("Downloading ... " );
req.setDescription("Downloading " + "cccc.jpg");
req.setVisibleInDownloadsUi(true);
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/Kopi81/" + "cccc.jpg");
refid = downloadManager.enqueue(req);
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Download completed !",Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Kopi81")
.setContentText("Download completed !");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(455, mBuilder.build());
}
};}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I tested on my device [Sony Z3] & [Sony XZ] and had no problem !
good luck.
I want to "pack" two applications in one Android APK file. Requirements:
When I install an APK fil it installs to separate apps.
If I uninstall an app, the other app would still remain installed.
Can I do it like that?
First upload those apps which you want to be installed at Dropbox (only).
Now obtain links of those APK files and in the link replace dropbox.com/..... with d.dropboxusercontent.com/...
Now, make an app and place the below code at "onCreate" or somewhere,
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "AppName.apk";
destination += FileName;
final Uri uri = Uri.parse("file://" + destination);
String URL = "d.dropboxusercontent.com/...............";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setDescription(Main.this.getString(R.string.notification_description));
request.setTitle(Main.this.getString(R.string.app_name));
// Set destination
request.setDestinationUri(uri);
// Get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
// Set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
finish();
}
};
// Register receiver for when .apk download is complete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
If you want to install multiple apps, then you can apply this code multiple times by changing variable names.
Please comment if it worked.
I have this code for download by download manger,
code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(name);
request.setTitle("دانلود ویدیو");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/mlindr/101ideas/video", video);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//manager.enqueue(request);
long id = manager.enqueue(request);
Download class:
public static boolean isDownloadManagerAvailable(Context context) {
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
return false;
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName("com.android.providers.downloads.ui", "com.android.providers.downloads.ui.DownloadList");
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
} catch (Exception e) {
return false;
}
}
Now I want when click on download processing in notification, current download be cancelled.
If you want to cancel download action to the download notification you need to use custom notification instead of built-in notification.
or you can open download list with cancle action one the user click in the notification
You can read more about download manager click action here
Hi i'm downloading a file and i'm wanting to fire an ACTION_VIEW Intent once its finished downloading. so does anyone know how to detect when DownloadManager has finished.
heres how i'm running DownloadManager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
ContextWrapper c = new ContextWrapper(getBaseContext());
String filePath = c.getFilesDir().getPath();
request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();
BroadcastReceiver onComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String path = getFilesDir().getPath() +"/" + filename;
File f = new File(path);
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(f));
startActivity(myIntent);
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
It looks OK for me. That example is not working? But different thing is how you fire next Intent in onReceive method. Please try use your action in
myIntent = new Intent(YourActionClass.class);
Figured it out myself i needed to set the mimeType so it knows which apps to look for