i made app with webView in Android Studio , my app contains all images so i have also implemented download button in my app , but when someone click on download button it just download the image , i want to show pop up when download is Finished , something like this,
Download Completed and below Ok button
this is my code of download
Uri source = Uri.parse(url);
// Make a new request pointing to the .apk url
DownloadManager.Request request = new DownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
request.setTitle("PunjabiDharti.jpg");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "PunjabiDharti.jpg");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
how to show pop up ?
First, add Dependencies for MaterialDialog in build.gradle
dependencies {
// ... other dependencies here
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
}
Create a BroadcastReceiver Handler
BroadcastReceiver downloadListener = new BroadcastReceiver(){
public void onReceive(Context ct, Intent intent){
new MaterialDialog.Builder(this)
.title("Download Completed")
.content("Download Successfully Completed")
.positiveText("OK")
.show();
}
};
And now register Receiver
registerReceiver(downloadListener, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Related
I want to download a pfd file when ever a specific notification is pushed via FCM.
I have wrote a service which extends FirebaseMessagingService and in onMessageReceived event after showing the notification I want to download the file. This process only works when application is active when application is in background it does not work.
Is this because of a restriction or you need a work around to achieve this?
Basic Idea is to send a push notification with a URL in it and accessing the URL in onMessageReceived will download a file from web server. Even if the application is stopped/ in background.
Here is the code.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
downloadPdf("www.dummmy.file/file.pdf");
}
public void downloadPdf(String url) {
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setTitle("My File");
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"downloadfileName");
downloadmanager.enqueue(request);
}
Here i used DownloadManger to download and it will show the progress in Notification bar
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
registerReceiver(downloadReceiver, filter);
Broadcast receiver to receive click action(DownloadManager.ACTION_NOTIFICATION_CLICKED) during downloading and the action is working during download
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action))
{
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
}
};
but after download DownloadManager.ACTION_NOTIFICATION_CLICKED is not working to redirect to downloads app.
Can any one help me how can i redirect after clicking on DownloadManger notification(after complete download) to downloads app ????????????
This is the code I'm currently using
String iosjiUrl = "http://modapps.com/NotoCoji.ttf";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(iosjiUrl));
request.setDescription("Sscrition");
request.setTitle("Somle");
// 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);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "NotoColorji.ttf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
I'm not able to find a way for me to wait until the file is downloaded.
I also tried figuring out how to go about doing ASync but couldn't figure that out either =/
Thanks again!
Use a BroadcastReceiver to detect when the download finishes:
public class DownloadBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//Show a notification
}
}
}
and register it in your manifest:
<receiver android:name=".DownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
A Broadcast intent action sent by the download manager when a download completes so you need to register a receiver for when the download is complete:
To register receiver
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
and a BroadcastReciever handler
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
// your code
}
};
You can also create AsyncTask to handle the downloading of big files
Create a download dialog of some sort to display downloading in notification area and than handle the opening of the file:
protected void openFile(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE");
startActivity(install);
}
you can also check the sample link
sample
Solution: API 11 is needed see answer below!
Easy Question: After downloading a File with the implemented DownloadManager the Notification disappears. How do I force the Notification to stay after Download?
I tried to use VISIBILITY_VISIBLE_NOTIFY_COMPLETED, but i do not know how i can use it
Thank for any kind of help to solve this problem ;)
EDIT: Code
public class BgDL extends Activity {
private DownloadManager mgr = null;
private long id;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(getIntent().getStringExtra("URL")));
id = mgr.enqueue(request
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "UPDATE")
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("APP update")
.setDescription("New version "+getIntent().getDoubleExtra("OV", 0.0))
);
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
BroadcastReceiver receiver = new BroadcastReceiver () {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mgr.ACTION_DOWNLOAD_COMPLETE) ){
unregisterReceiver(receiver);
finishActivity(99);
}
}
};
}
Add the correct flag to your request:
Request request = new Request(Uri.parse(getIntent().getStringExtra("URL")));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Reference:
http://developer.android.com/reference/android/app/DownloadManager.Request.html#setNotificationVisibility(int)
Control whether a system notification is posted by the download manager while this download is running or when it is completed. If enabled, the download manager posts notifications about downloads through the system NotificationManager. By default, a notification is shown only when the download is in progress.
http://developer.android.com/reference/android/app/DownloadManager.Request.html#VISIBILITY_VISIBLE_NOTIFY_COMPLETED
This download is visible and shows in the notifications while in progress and after completion.
I am writing an application targeting API level 9 or higher. So, i
have decided to go with DownloadManager Class that SDK offers.
My question is 2 part -
1. When i am downloading a single file, how do i display the progress
of the download. I see i can get COLUMN_TOTAL_SIZE_BYTES and
COLUMN_BYTES_DOWNLOADED_SO_FAR from the querying the download manager
instance. But i am not sure if i have to put the query in a thread and
implement a loop so that i can poll regularly to update the progress
bar. I guess, i am not sure, how to query regularly - will it go in
the main thread or be implemented as a runnable - the mechanism i am
not clear.
2. If i have to support multiple file downloads, then do i have to
launch each one of them in it's own thread?
Thanks.
If you are building on 2.3 and above you don't need to, it automatically displays it in the status bar. Do it like this,
Code from commonsware,
private static final int DOWNLOAD_SUCCESSFUL = 100;
private static final int DOWNLOAD_FAILED = 99;
private DownloadManager mgr=null;
on create do this,
`mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
`
Then on the click event of the file download,
lastDownload =
mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Test File")
.setDescription("Download zipped file.")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
file_name));
And then a broadcast receiver for when your donwnload compeletes,
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
findViewById(R.id.start).setEnabled(true);
File unzipFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),file_name);
//check for new file download
//extract if it's a new download
if (unzipFile.exists()) {
new UnzipFile().execute();
} else {
Toast.makeText(Main.this, "Download not found!", Toast.LENGTH_LONG).show();
}
}
};`