-
-
Save billmote/0ee30ff348677cf3a2d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver | |
android:name=".DownloadBroadcastReceiver" | |
android:exported="true"> | |
<intent-filter> | |
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" /> | |
</intent-filter> | |
</receiver> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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