在Android Q设备上更新我正在运行的Xamarin应用的APK文件

eufgjt7s  于 9个月前  发布在  Android
关注(0)|答案(1)|浏览(65)

我正在开发一个应用程序,从服务器下载一个较新的APK文件。下载到存储工作正常,现在它应该在没有用户交互的情况下更新。
我已经阅读了所有关于Android,Xamarin和StackOverflow上类似问题的文档。但我不能让它像预期的那样工作。没有安装意图,也没有来自源代码的异常,应用程序只是继续运行。你知道我错过了什么吗
MainActivity.cs

public bool Install(string filePath)
{
    Java.IO.File file = new Java.IO.File(filePath);
    if (!file.Exists())
    {
        return false;
    }

    // please allow permission for package install
    if (!global::Android.App.Application.Context.PackageManager.CanRequestPackageInstalls())
    {
        var intent = new Intent();
        intent.SetAction(global::Android.Provider.Settings.ActionManageUnknownAppSources);
        intent.SetData(global::Android.Net.Uri.Parse("package:" + global::Android.App.Application.Context.PackageName));
        StartActivity(intent);
    }

    try
    {
        // Android 10.x and newer
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
        {
            global::Android.Net.Uri uri = FileProvider.GetUriForFile(global::Android.App.Application.Context, global::Android.App.Application.Context.PackageName.ToString() + ".provider", file);
            InstallPackageAndroidQAndAbove(file.AbsolutePath, uri);
        }

        // omitted older Android versions, working
        else if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { }
        else { }
    }
    catch (Exception e)
    {
        return false;
    }
    return true;
}

public void InstallPackageAndroidQAndAbove(string filePath, global::Android.Net.Uri apkUri)
{
    var packageInstaller = global::Android.App.Application.Context.PackageManager.PackageInstaller;
    var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
    sessionParams.SetAppPackageName(global::Android.App.Application.Context.PackageName);
    if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
    {
        sessionParams.SetRequireUserAction( (int)global::Android.Content.PM.PackageInstallUserAction.NotRequired );
    }
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
    {
        sessionParams.SetPackageSource((int)global::Android.Content.PM.PackageSource.LocalFile);
    }

    int sessionId = packageInstaller.CreateSession(sessionParams);
    var session = packageInstaller.OpenSession(sessionId);

    AddApkToInstallSession(filePath, session);
          
    // Create an install status receiver.
    var intent = new Intent(this, this.Class);
    intent.SetAction(Intent.ActionInstallPackage);
    intent.SetData(apkUri);
    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.NoHistory | ActivityFlags.ExcludeFromRecents | ActivityFlags.GrantReadUriPermission);
    global::Android.App.Application.Context.StartActivity(intent);

    var pendingIntent = PendingIntent.GetActivity(global::Android.App.Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);
    var observer = new PackageInstallObserver(packageInstaller);
    observer.InstallFailed += OnInstallFailed;
    packageInstaller.RegisterSessionCallback(observer);
    global::Android.App.Application.Context.SendBroadcast(intent);

    // Commit the session (this will start the installation workflow).
    // -> But nothing happens here, there is no intent on my device, 
    // -> observer does not handle InstallFailed, and no exception is thrown
    session.Commit(pendingIntent.IntentSender);      
}
        
                
private async static void AddApkToInstallSession(string filePath, PackageInstaller.Session session)
{
    using (var input = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        using (var packageInSession = session.OpenWrite("com.Westfalia.Savanna.CrossClient", 0, -1))
        {
            await input.CopyToAsync(packageInSession);
            packageInSession.Close();
        }
        input.Close();
    }

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
}
        
private void OnInstallFailed(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

具有AndroidManifest.xml中的权限

  • 安装包安装包
  • 读取外部存储
  • 请求删除_包
  • 请求安装包
  • RESTARTPackages
  • 无用户操作的数据包

我在三星和霍尼韦尔的真实的设备上测试了Android > 10。
我期望安装意图和重新启动正在运行的应用程序。

eiee3dmh

eiee3dmh1#

你的问题表明你不使用Playstore或类似的东西。
我建议在这里看看:How to autoupdate android app without playstore? Like Facebook app or any Contest app
自动更新程序将是您需要在运行时更新应用程序的库。https://github.com/NDMAC/android-auto-updater-client
由于Auto Update是一个Java库,因此您必须绑定它:https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/

相关问题