簡體   English   中英

無法解析具有“下載”權限的內容提供程序時,PackageInstaller 崩潰

[英]PackageInstaller crash when unable to resolve content provider with "downloads" authority

對於一些背景知識,我正在為一家企業在 Xamarin 中編寫一個 Android 應用程序,它應該能夠自我更新,而不是通過 Play 商店。 特別是有一種設備無法自行更新,我不知道為什么。

更新代碼如下所示:

            try
            {
                var intent = new Intent(Intent.ActionInstallPackage);
                intent.SetFlags(ActivityFlags.NewTask);
                var file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.Path + "/com.mycompany.appname.apk");
                var uri = FileProvider.GetUriForFile(this, "com.mycompany.appname.update_file_provider", file);
                GrantUriPermission("com.google.android.packageinstaller", uri, ActivityFlags.GrantReadUriPermission);
                intent.SetDataAndType(uri, "application/vnd.android.package-archive");
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
                var notification = GetNotificationBuilder(installedVersion, latestVersion, this)
                    .SetContentText("Download complete.")
                    .AddAction(new NotificationCompat.Action(Resource.Drawable.ic_note_add_black_24dp, "Install", pendingIntent))
                    .SetAutoCancel(true)
                    .Build();
                NotificationManagerCompat.From(this).Notify(NotificationId, notification);
                StopSelf();
            }
            catch (Exception e)
            {
                var notification = GetNotificationBuilder(installedVersion, latestVersion, this)
                    .SetContentText("Failed to install update: " + e.Message)
                    .SetAutoCancel(true)
                    .Build();
                NotificationManagerCompat.From(this).Notify(NotificationId, notification);
                StopSelf();
                return;
            }

當它運行時,我收到一條消息,說“PackageInstaller 已停止”。 堆棧跟蹤顯示這是由於包安裝程序應用程序中的 NPE 導致的:

12-02 17:37:17.153  4897  4897 E AndroidRuntime: FATAL EXCEPTION: main
12-02 17:37:17.153  4897  4897 E AndroidRuntime: Process: com.google.android.packageinstaller, PID: 4897
12-02 17:37:17.153  4897  4897 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.InstallStart}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.pm.ComponentInfo.getComponentName()' on a null object reference
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2785)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread.-wrap11(Unknown Source:0)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6537)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:465)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
12-02 17:37:17.153  4897  4897 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.pm.ComponentInfo.getComponentName()' on a null object reference
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at com.android.packageinstaller.InstallStart.isSystemDownloadsProvider(InstallStart.java:230)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at com.android.packageinstaller.InstallStart.getOriginatingUid(InstallStart.java:222)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at com.android.packageinstaller.InstallStart.onCreate(InstallStart.java:73)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:7023)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:7014)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2738)
12-02 17:37:17.153  4897  4897 E AndroidRuntime:        ... 9 more

鄭重聲明:此特定設備運行的是 Android 8.1。 我已經在其他運行 Android 8.0、Android 10 和 Android 7 的設備上進行了測試,沒有出現任何問題,但我沒有任何其他具有該確切 Android 版本的設備進行測試。

編輯我意識到我應該多解釋一下標題。 我實際上已經查看了 PackageInstaller 應用程序源代碼,圍繞着堆棧跟蹤出現的位置,似乎這一行是這個 NPE 的原因:

final String downloadProviderPackage = getPackageManager().resolveContentProvider(
                DOWNLOADS_AUTHORITY, 0).getComponentName().getPackageName();

DOWNLOADS_AUTHORITY是一個值為"downloads"的常量字符串,對resolveContentProvider()的調用返回 null。 不清楚的是為什么這個特定的設備會發生這種情況,而不是其他設備,或者如何修復它。

問題原來是下載管理器已被禁用。 這似乎是僅在 android 8.0 和 8.1 中的問題。 我仍然不確定它是如何開始被禁用的,但我的解決方案是使用 blackapps 推薦的空檢查,並包括提示用戶在檢測到時重新啟用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM