Skip to content

Instantly share code, notes, and snippets.

@billmote
Forked from vincekennedy/AndroidManifest.xml
Last active February 8, 2022 12:34
Show Gist options
  • Save billmote/0ee30ff348677cf3a2d5 to your computer and use it in GitHub Desktop.
Save billmote/0ee30ff348677cf3a2d5 to your computer and use it in GitHub Desktop.
<receiver
android:name=".DownloadBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
public class DownloadListenerService extends BroadcastReceiver{
@Override
public void onReceive(final Context context, final Intent intent) {
DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
if (intent != null) {
Bundle extras = intent.getExtras();
DownloadManager.Query query = new DownloadManager.Query();
long downloadId = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
cursor.close();
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(new File(uri)), "application/vnd.android.package-archive");
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(installIntent);
}
}
}
}
}
public void downloadFile(String updateDownloadUrl) {
String url = updateDownloadUrl;
Uri downloadUrl = Uri.parse(updateDownloadUrl);
DownloadManager.Request downloadRequest = new DownloadManager.Request(downloadUrl);
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "FileName.apk");
downloadManager.enqueue(downloadRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment