0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
G. Attila
  • 81
  • 1
  • 8
  • have you tried https://stackoverflow.com/questions/39147608/android-install-apk-with-intent-view-action-not-working-with-file-provider/40131196 ? – Eduardo Speroni Apr 23 '19 at 00:02
  • Yeah, I have the tried those too. I think I have something with FileProvider, because I dont see the data in the error string. The output of the FileProvider.getUriForFile().toString() : content://org.nativescipt.ITPalert.provider/external_download/update.apk – G. Attila Apr 23 '19 at 00:42

2 Answers2

2

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)
    })
  • Thank's for your response, but it haven't solved my problem. As I wrote Im using android 8.0. And I still get an error. What do you think? It's caused by the File Provider? JS: [Error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.DEFAULT] typ=application/vnd.android.package-archive flg=0x1 } – G. Attila Apr 23 '19 at 13:07
  • import app = require("application"); var context = app.android.foregroundactivity; – Andre Bahtiar Fauzi Apr 23 '19 at 17:12
  • Yes, I have those imported :( – G. Attila Apr 23 '19 at 17:17
  • try to import this: var utils = require("utils/utils"); var context = utils.ad.getApplicationContext(); ref: https://docs.nativescript.org/core-concepts/utils – Andre Bahtiar Fauzi Apr 24 '19 at 17:29
1

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>
G. Attila
  • 81
  • 1
  • 8