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.
Related
I have written a method that check new version of my APP and when it exists, the application download and it should install that. But it can not install the downloaded application and it gives me this error:
There was a problem parsing the package.
But when I uninstall my app and install it again with the downloaded apk file. Everything works fine so my app downloads the file correctly.
But why this could not install it programmatically?
This is my Download code:
public long downloadFromUrl(String url, String fileName, String format) {
String fullFileName = fileName + format;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fullFileName)// Uri of the destination file
.setTitle(fullFileName)// Title of the Download Notification
.setDescription("Downloading...");// Description of the Download Notification
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);
if (format.equals(APK_FORMAT)) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fullFileName);
if(file.exists()){
DownloadReceiver downloadReceiver = new DownloadReceiver(file);
Intent intent = new Intent();
downloadReceiver.onReceive(context,intent);
context.registerReceiver(downloadReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
return id;
}
This is a receiver that when download is completed, runs:
#Override
public void onReceive(Context context, Intent intent) {
long receivedId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (file != null) {
Uri uri = FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName()+ ".provider",file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Uri uri;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", destination);
} else {
uri = Uri.fromFile(destination);
}
//Log.d("path",uri.getPath());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
make sure you have added this permission to your manifest file
Also make sure both version of your APKs are signed and with same key.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
I made it work with this code (destination is file object).
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 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.
****** 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..!
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