我的应用程序中有一个功能,允许用户下载新版本。
但是我的问题是,当我启动应该启动安装过程的意图时,出现一条消息:
解析错误。解析软件包时出现问题
这是我的代码
Button button;
String urlApp = "example.com/app.apk"
String fileName = "app.apk";
DownloadManager downloadManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(Environment.DIRECTORY_DOWNLOADS, fileName);
if (file.exists()){
boolean deleted = file.delete();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlApp));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("Click to update");
downloadManager.enqueue(request);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
});
}
private void openFile() {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
openFile();
}
};
}
仅适用于.apk文件,.txt文件可以正常工作。logcat上没有错误。删除也不起作用,但这不是我的主要问题。
提前致谢
抱歉,这是一个愚蠢的问题,但这是我第一次与下载管理器合作
好,我终于解决了
代替:
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
我用了
install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+fileName)),
"application/vnd.android.package-archive");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句