'starting an apk in a android delphi seattle application error

For several reasons I don't want to use the Google Play mechanism to upgrade an android application. So I decided to make my own system to download and upgrade an android application.

Step 1: download the update .apk file.

Step 2: put a button in the original application so a user can click on it to start the .apk for upgrade.

The following code is giving me an error.

  procedure TfrmUpdateProgram.DoUpgrade;  
  {$IF DEFINED(IOS) or DEFINED(ANDROID)}
    var Intent : JIntent;
  {$ELSE}
  {$ENDIF}
  begin
    {$IF DEFINED(IOS) or DEFINED(ANDROID)}
      Intent := TJIntent.Create;
      Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
      Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
      Intent.setDataAndType(StrToJURI(newAPKFile), StringToJString('application/vnd.android.package-archive'));

      SharedActivityContext.startActivity(Intent);
      Application.Terminate;

    {$ELSE}
      ShellExecute(0, PWideChar('Open'), PWideChar(newAPKFile), nil, nil, SW_HIDE);
    {$ENDIF}
  end;

android.content.activitynotfoundexception: No activity found to handle intent {act=android.intent.action.view dat=/storage.....update.apk typ=application/vnd.android.package-archive flg=0x1000000}

Any thoughts?



Solution 1:[1]

Try removing the call to Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK), it is not strictly needed.

Make sure the .apk file is world-readable before launching the Intent, unless you store it to a public external folder. And make sure it is accessible via a file: URI.

And make sure your Android device is configured to allow apps to be installed from unknown sources.

Solution 2:[2]

    var
        Path: JString;
        Name: JString;
        F: Jfile;
        Intent: JIntent;
        FileName, DestFileName: string;



     FileName := System.IOUtils.TPath.GetDownloadsPath + PathDelim + 'myAPK_name.apk';
     Intent := TJIntent.Create;
     Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
     if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.N then
     begin

          lFile := TJFile.JavaClass.init(StringToJString(FileName));

          Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION + TJIntent.JavaClass.FLAG_GRANT_WRITE_URI_PERMISSION);
          Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
         //This line used to be in Delphi 10.4 
         //Data := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,StringToJString('com.embarcadero.BarcodexMS.fileprovider'), lFile);
         //Use Following line in Delphi 11.1
         Data := TJcontent_FileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider'), lFile);
     end
     else
     begin
          Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + FileName));
     end;


     Intent.setDataAndType(Data, StringToJString('application/vnd.android.package-archive'));
     TAndroidHelper.Activity.startActivity(Intent);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau
Solution 2 mesutuk