1

I've got problem about intent in the Xamarin.Forms. I want to install downloaded APK file,which stored in the Internal Storage root. I have written code and it works perfectly on the Android 6 but it doesn't work on the Android 8 and 9. I used FileProvider class,I know that it's a way for Android 7+ in this case,but it still doesn't work.

no any error,it's opening packageInstaller and closing immediately,but when I tried absolutely same code in the new project,it works perfectly.

I checked,when file name is incorrect it's showing parsing error. also I checked that File.EXists method returns true with this APK file

I have no ideas,why happens it

tried update Xamarin.Forms NuGet

******MainActivity******

private void DetailPage_openit()
        {
            CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Storage);
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(FileProvider.FileProvider.GetUriForFile(this, "aaaa", new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/gcam14.apk")), "application/vnd.android.package-archive");
            intent.SetFlags(ActivityFlags.NewTask);
            intent.SetFlags(ActivityFlags.GrantReadUriPermission);
            StartActivity(intent);


        }

******AndroidManifest.xml******

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:targetSandboxVersion="1" android:versionCode="1" android:versionName="1.0" package="com.granturismo.gcamator" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />

  <application android:label="Gcamator" android:usesCleartextTraffic="true" android:icon="@drawable/logo" android:allowBackup="false">
    <provider
          android:name="android.support.v4.content.FileProvider"
          android:authorities="aaaa"
          android:exported="false"
          android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
  </provider>    
    <uses-library android:name="org.apache.http.legacy" android:required="false" /> 

  </application>
  <uses-permission android:name="android:permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

******provider_paths.xml******

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
</paths>

1 Answers1

0

Before Android8.0 (android-o) : can be installed, or need to open the system Settings "install unknown application" permission, or there will be a popup to give the user a hint. ; After Android8.0, Google has further strengthened the permission management. The permanent switch of "install unknown application" permission is removed. Every time when users install the App from the location, they need to independently authorize and manually confirm the permission of the software. When we install Apk on Android8.0, we will not jump to the system App installation interface without setting or code control, which will result in the App cannot be installed.

try this :

 if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == Permission.Granted && ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted)
      {
        Intent intent = new Intent(Intent.ActionView);
        intent.AddFlags(ActivityFlags.GrantReadUriPermission);
        intent.AddFlags(ActivityFlags.NewTask);
        intent.SetDataAndType(FileProvider.GetUriForFile(this, "aaaa", new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads) + "/gcam14.apk")), "application/vnd.android.package-archive");
        if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
            {
                bool hasInstallPermission = PackageManager.CanRequestPackageInstalls();
                if (!hasInstallPermission)
                {
                    //Request permission to install unknown application source
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.RequestInstallPackages }, 1);
                }
            }

           StartActivity(intent);

      }
   else
      {

         ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 1);
      }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I have just tried xamarin.forms Launcher class and it also doesn't work but after that I tried launch JPG file which stored same location where APK is and it was opened. – კუპა Oct 02 '19 at 18:47
  • I tried it and I got error about declare permission I checked manifest file, there was Install Permission line but in the properties,in Android Manifest tab I saw that there was not checked Install Permission checked it and problem solved. thank you very much – კუპა Oct 03 '19 at 09:33
  • I have edited comment. problem solved. thank you once again – კუპა Oct 03 '19 at 09:35