How to programatically remove notification in download manager android - android

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

Related

Installation dialog not shown in oreo when done programatically if device is not rooted

I am trying to update app after showing the alert dialog in my app. It is working fine below Android Oreo. Here is what I have tried till now
String ANDROID_PACKAGE = "application/vnd.android.package-archive";
File update_apk = new File(context.getFilesDir(), intent.getStringExtra("update_file"));
Intent notificationIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
notificationIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
notificationIntent.setDataAndType(
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", update_apk),
ANDROID_PACKAGE);
} else {
notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setDataAndType(
Uri.parse("file://" + update_apk.getAbsolutePath()),
ANDROID_PACKAGE);
}
startActivity(notificationIntent);
How to show this app update dialog in the devices which are not rooted. Any help would be appreciated. Thanks in aadvance.
Check below permission in your manifest.xml file.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Also check your file provider for Intent.VIEW_ACTION.
More reference check this page Android install apk with Intent.VIEW_ACTION not working with File provider
enjoy code.

How to solve DownloadManager status pending?

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.

Two Android apps in one file

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.

No Activity found to handle Intent when using FileProvider

In my app I have a custom auto download and install APK it works like this
// auto register for the complete download
activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// Download the file through DownloadManager
String destination = Environment.getExternalStorageDirectory() + "/";
String fileName = "myfile.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setDescription("description");
request.setTitle("title");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
// BEFORE working doing this
//install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//install.setDataAndType(uri,
// manager.getMimeTypeForDownloadedFile(downloadId));
// Using file provider it doesnt work
Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
"com.myapp", file);
install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
activity.unregisterReceiver(this);
}
};
Android manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
Provider_path (Sorry for some reason so cuts the path tag)
external-path name="myfolder" path="."/>
When the file finished to download the onComplete is called but the activiy doesn't start:
No Activity found to handle Intent { act=android.intent.action.VIEW
dat=content://com.myapp/myfolder/myfile.apk
typ=application/vnd.android.package-archive flg=0x4000000 }
When using the normal file:// it does work
Is there something I'm missing when using the file provider? Does the activity doesn't start because the file is not found ?
Do I need extra permission ? (at the moment I have INTERNET, READ and WRITE on external storage)
The package installer only supports content schemes starting on Android 7.0. Prior to that — and despite documentation to the contrary — the package installer only supports file schemes.
You will need to set the Uri on your Intent differently based on whether you are running on Android 7.0+ or not, such as by branching on Build.VERSION.SDK_INT.
Commonware is right,
For those looking for the code:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
downloadButton.setEnabled(true);
downloadButton.setText("Terminado");
progressbar.setVisibility(View.GONE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri apkURI = FileProvider.getUriForFile(
self,
self.getApplicationContext()
.getPackageName() + ".provider", file);
install.setDataAndType(apkURI,
manager.getMimeTypeForDownloadedFile(downloadId));
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(install);
} else{
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "surveyevent.apk";
destination += fileName;
Uri uri = Uri.parse("file://" + destination);
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();
}
};
Your provider is not found because
android:enabled="true"
is missing in your manifest.
But then it will not work either reading the other answer of CW.

DownloadManager, never ending notification

****** 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..!

Categories

Resources