Reputation: 91
I'm making an app for my clients, and because of that I don't want to upload it to play store. Because of the user experience I want to make the update process automatic, currently I can download the apk correctly in my documents folder. I searched the google 4 days but I cant get my code working. Im on API level 28. I would like to install the updates in the background, but if the user needs to click install that ok too.
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:
[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)
Currently Im using android 8.0. However I modify my code I get this error, if I open the file in filebrowser I can install it manually.
Upvotes: 0
Views: 1568
Reputation: 91
Hi I have found somehow the answer. My code:
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"))
In the manifest file I added:
<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>
Upvotes: 1
Reputation: 126
Instead of using android.content.Intent.ACTION_INSTALL_PACKAGE
, use android.content.Intent.ACTION_VIEW
to install apk.
For Android API < 24, you don't need to use File provider. check code below
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)
})
Upvotes: 2