找不到处理意图的活动

找不到处理意图的活动

本文介绍了Xamarin Android 10 安装 APK - 找不到处理意图的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 xamarin android 应用程序的文件系统安装第 3 方应用程序.我在 Android 10 之前成功使用的代码非常简单明了.

I want to install a 3rd party app from the filesystem from my xamarin android app. The code I used successfully before Android 10 was pretty straightforward and easy.

            Intent intent = new Intent(Intent.ActionView);
            Uri data = Uri.FromFile(file);

            intent.SetDataAndType(data, "application/vnd.android.package-archive");

            context.StartActivity(intent);

此代码在 Android 10 上不起作用,因为 ACTION_VIEW 和 ACTION_INSTALL_PACKAGE 在 Android 10 中已弃用.看起来我们现在需要使用 PackageInstaller API.

This code does not work on Android 10 because of ACTION_VIEW and ACTION_INSTALL_PACKAGE were deprecated in Android 10. Looks like we now need to use the PackageInstaller API.

我尝试使用 PackageInstaller API 编写方法.不幸的是它不起作用.

I tried to write a method using the PackageInstaller API. Unfortunately it doesn't work.

使用 PackageInstaller API 编写代码

Code with PackageInstaller API

 public static void InstallPackageAndroidQAndAbove(Context context, string filePath, string packageName)
    {

        var packageInstaller = context.PackageManager.PackageInstaller;
        var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
        sessionParams.SetAppPackageName(packageName);
        int sessionId = packageInstaller.CreateSession(sessionParams);
        var session = packageInstaller.OpenSession(sessionId);

        var input = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var output = session.OpenWrite(packageName, 0, -1);

        input.CopyTo(output);

        output.Close();
        input.Close();
        input.Dispose();

        session.Fsync(output);

        var pendingIntent = PendingIntent.GetBroadcast(context, sessionId, new Intent(Intent.ActionInstallPackage), 0);
        session.Commit(pendingIntent.IntentSender);

    }

调用过程中出现异常无法识别的流".

An exception "Unrecognized stream" occurs during the call.

希望有人能帮助我.

非常感谢您.

推荐答案

用于使用 PackageInstaller API 安装 apk 的 Xamarin Android 代码.

Xamarin Android Code for installing an apk with the PackageInstaller API.

//Change to your package name
    const string PACKAGE_INSTALLED_ACTION =
                "com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED";

     public static void InstallPackageAndroidQAndAbove(Context context, string filePath)
            {

                var packageInstaller = context.PackageManager.PackageInstaller;
                var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
                int sessionId = packageInstaller.CreateSession(sessionParams);
                var session = packageInstaller.OpenSession(sessionId);

                addApkToInstallSession(filePath, session);

                // Create an install status receiver.
                Intent intent = new Intent(context, context.Class);
                intent.SetAction(Globals.PACKAGE_INSTALLED_ACTION);
                PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, 0);
                IntentSender statusReceiver = pendingIntent.IntentSender;

                // Commit the session (this will start the installation workflow).
                session.Commit(statusReceiver);

            }

     private static void addApkToInstallSession(string filePath, PackageInstaller.Session session)
            {
                using (var input = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    using (var packageInSession = session.OpenWrite("package", 0, -1))
                    {
                        input.CopyTo(packageInSession);
                        packageInSession.Close();
                    }
                    input.Close();
                }
                //That this is necessary could be a Xamarin bug.
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();

            }

// Note: this Activity must run in singleTop launchMode for it to be able to receive the //intent
     protected override void OnNewIntent(Intent intent)
            {
                base.OnNewIntent(intent);

                Bundle extras = intent.Extras;

                if (Globals.PACKAGE_INSTALLED_ACTION.Equals(intent.Action))
                {
                    int status = extras.GetInt(PackageInstaller.ExtraStatus);
                    String message = extras.GetString(PackageInstaller.ExtraStatusMessage);

                    switch (status)
                    {
                        case (int)PackageInstallStatus.PendingUserAction:
                            // This test app isn't privileged, so the user has to confirm the install.
                            Intent confirmIntent = (Intent)extras.Get(Intent.ExtraIntent);
                            StartActivity(confirmIntent);
                            break;
                        case (int)PackageInstallStatus.Success:
                            Toast.MakeText(this, "Install succeeded!", ToastLength.Long).Show();
                            break;
                        case (int)PackageInstallStatus.Failure:
                        case (int)PackageInstallStatus.FailureAborted:
                        case (int)PackageInstallStatus.FailureBlocked:
                        case (int)PackageInstallStatus.FailureConflict:
                        case (int)PackageInstallStatus.FailureIncompatible:
                        case (int)PackageInstallStatus.FailureInvalid:
                        case (int)PackageInstallStatus.FailureStorage:
                            Toast.MakeText(this, "Install failed! " + status + ", " + message,
                                    ToastLength.Long).Show();
                            break;
                        default:
                            Toast.MakeText(this, "Unrecognized status received from installer: " + status,
                                   ToastLength.Long).Show();
                            break;
                    }
                }


            }

这篇关于Xamarin Android 10 安装 APK - 找不到处理意图的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:58