简体   繁体   中英

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?

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.

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM