下载与应用程序的下载管理器和退出后安装APK [英] Install apk after download with download manager and exit from app
问题描述
我已经创建了一个Android应用程序,并从服务器上是否有新版本发布了它会自动启动,通过使用内置的下载管理器下载。自动安装完成后,下载我创建了一个广播接收器,通知下载已完成,完成,然后我开始安装。期间,我留在应用程序,不要关闭它,它工作正常。但我的问题是,当我关闭应用程序。所以下载完成后,我想自动安装它。但我不会发生。我应该为这个问题呢?
私人无效下载(字符串link){
DownloadManager.Request要求=新DownloadManager.Request(Uri.parse(链接));
request.setDescription(新版本);
request.setTitle(应用程序名称);
request.setMimeType(应用程序/ vnd.android.package归档);
request.setDestinationInExternalFilesDir(getApplicationContext(),Environment.DIRECTORY_DOWNLOADSmyapk.apk);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); downloadID = dm.enqueue(请求);
BR =新的广播接收器(){ @覆盖
公共无效的onReceive(上下文C,意图我){
字符串行动= i.getAction(); 如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){ DownloadManager.Query查询=新DownloadManager.Query();
query.setFilterById(downloadID); 光标downloadResult = dm.query(查询); 如果(downloadResult.moveToFirst()){
INT statusColumnIndex = downloadResult.getColumnIndex(DownloadManager.COLUMN_STATUS);
INT状态= downloadResult.getInt(statusColumnIndex); 如果(状态== DownloadManager.STATUS_SUCCESSFUL){
//下载成功完成
INT localFileNameId = downloadResult.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME); 字符串downloadPathFile = downloadResult.getString(localFileNameId);
串downloadPathDir = downloadPathFile.substring(0,downloadPathFile.lastIndexOf(/)+ 1);
串downloadName = downloadPathFile.substring(downloadPathFile.lastIndexOf(/)+ 1); Log.i(NAME =downloadName); 档案文件=新的文件(downloadPathDir);
文件[] =文件file.listFiles();
对于(F文件:文件){
如果(f.isFile()及&放大器; f.exists()及&放大器;!f.getName()等于(downloadName)){
f.delete();
}
} 意向意图=新意图(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(新文件(downloadPathFile)),应用程序/ vnd.android.package归档);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
unregisterReceiver(BR);
}
}
}
}
}; registerReceiver(BR,新的IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));}
您可以做这样的事情:
在您的清单中。
<接收机器人:名字=packageName.DownloadReceiver机器人:出口=真正的>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.DOWNLOAD_COMPLETE/>
&所述; /意图滤光器>
< /接收器>
,然后在 DownloadReceiver.java
添加到处理自动安装例如逻辑
公共类DownloadReceiver扩展广播接收器{ @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction();
如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
//添加你的逻辑在这里
}
}
}
i have created an android app and from server if any new version had released it will automatically start downloading by using inbuilt 'download manager' . for auto install after finish the download i have created a broadcast receiver to inform that download has finished and completed and then i start to install it. it works fine during i stay in app and don't close it. but my problem is when i close the application . so after finishing the download i want to automatically install it. but i wont happen. what should i do for this issue?
private void download(String link) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
request.setDescription("new version");
request.setTitle("app name");
request.setMimeType("application/vnd.android.package-archive");
request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, "myapk.apk");
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadID = dm.enqueue(request);
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
String action = i.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadID);
Cursor downloadResult = dm.query(query);
if (downloadResult.moveToFirst()) {
int statusColumnIndex = downloadResult.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = downloadResult.getInt(statusColumnIndex);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
//download completed successfully
int localFileNameId = downloadResult.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String downloadPathFile = downloadResult.getString(localFileNameId);
String downloadPathDir = downloadPathFile.substring(0, downloadPathFile.lastIndexOf("/") + 1);
String downloadName = downloadPathFile.substring(downloadPathFile.lastIndexOf("/") + 1);
Log.i("name =", downloadName);
File file = new File(downloadPathDir);
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile() && f.exists() && !f.getName().equals(downloadName)) {
f.delete();
}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(downloadPathFile)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
unregisterReceiver(br);
}
}
}
}
};
registerReceiver(br, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
You could do something like this:
in your manifest
<receiver android:name="packageName.DownloadReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
and then in DownloadReceiver.java
add the logic to handle auto install for example
public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
// add your logic here
}
}
}
这篇关于下载与应用程序的下载管理器和退出后安装APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!