Last active
August 29, 2015 14:20
-
-
Save vincekennedy/0b4795be8bc0f570e94e to your computer and use it in GitHub Desktop.
Download and initiate install of apk with Download Manager
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