您好,我正在为我的客户制作应用程序,因此,我不想将其上传到商店。由于用户体验的原因,我想使更新过程自动进行,目前,我可以在我的文档文件夹中正确下载apk。我在Google上搜索了4天,但无法正常工作。我的API级别为28。我想在后台安装更新,但是如果用户也需要单击“确定”安装。
var filePath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString();
filePath = fileSystemModule.path.join(filePath, "update.apk");
httpModule.getFile('http://192.168.1.11:8000/' + "android/update.apk", filePath).then((resultFile) => {
Permissions.requestPermissions(android.Manifest.permission.REQUEST_INSTALL_PACKAGES)
try {
const context = utils.ad.getApplicationContext();
var intent = new android.content.Intent(android.content.Intent.ACTION_INSTALL_PACKAGE);
var data = android.support.v4.content.FileProvider.getUriForFile(application.android.context, "org.nativescipt.ITPalert.provider", new java.io.File(filePath))
intent.setData(data);
intent.setType("application/vnd.android.package-archive");
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
intent.setFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(intent)
}
catch(e) {
console.log(e)
}
}, (e) => {
console.log('error' + e)
})
file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="just_a_name" path=""/>
<external-path name="external_files" path="."/>
</paths>
AndroidManifest.xml
...
<application ...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="org.nativescipt.ITPalert.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
错误:
[Error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSTALL_PACKAGE typ=application/vnd.android.package-archive flg=0x1 }
JS: android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1968)
JS: android.app.Instrumentation.execStartActivity(Instrumentation.java:1622)
JS: android.app.ContextImpl.startActivity(ContextImpl.java:868)
JS: android.app.ContextImpl.startActivity(ContextImpl.java:845)
目前我使用的是android 8.0。但是我修改了代码,但出现此错误,如果我在filebrowser中打开文件,则可以手动安装。
答案 0 :(得分:1)
使用android.content.Intent.ACTION_INSTALL_PACKAGE
来安装APK,而不要使用android.content.Intent.ACTION_VIEW
。
对于Android API <24,您不需要使用文件提供程序。检查下面的代码
var filePath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString();
filePath = fileSystemModule.path.join(filePath, "update.apk");
httpModule.getFile('http://192.168.1.11:8000/' + "android/update.apk", filePath).then((resultFile) => {
Permissions.requestPermissions(android.Manifest.permission.REQUEST_INSTALL_PACKAGES)
try {
const context = utils.ad.getApplicationContext();
if(platform.device.sdkVersion < 24) {
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setData(android.net.Uri.fromFile(new java.io.File(filePath)));
intent.setType("application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
} else {
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
var data = android.support.v4.content.FileProvider.getUriForFile(application.android.context, "org.nativescipt.ITPalert.provider", new java.io.File(filePath))
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(data);
intent.setType("application/vnd.android.package-archive");
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
intent.setFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(intent);
}
}
catch(e) {
console.log(e)
}
}, (e) => {
console.log('error' + e)
})
答案 1 :(得分:0)
嗨,我已经找到答案了。 我的代码:
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW)
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Permissions.requestPermission(android.Manifest.permission.REQUEST_INSTALL_PACKAGES)
var imagePath = new java.io.File(android.os.Environment.getExternalStorageDirectory(), "Download")
var newFile = new java.io.File(imagePath, "update.apk")
var data = android.support.v4.content.FileProvider.getUriForFile(application.android.context, "org.nativescript.ITPalert.provider", newFile)
intent.setDataAndType(data, "application/vnd.android.package-archive")
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
else {
var fileLocation = new java.io.File(application.android.context.getFilesDir(), "Download/update.apk")
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
intent.setDataAndType(android.net.Uri.fromFile(fileLocation), "application/vnd.android.package-archive")
}
application.android.context.startActivity(android.content.Intent.createChooser(intent, "asd"))
在清单文件中我添加了:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="org.nativescript.ITPalert.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
Filepath.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="Download/"/>
</paths>