[英]Android PackageInstaller not installing APK
Hello StackOverflow users,你好 StackOverflow 用户,
i have an Android app outside of the Play Store.我在 Play 商店之外有一个 Android 应用程序。 It updates itself by downloading a new APK and invoking the installer dialog using an Intent.
它通过下载新的 APK 并使用 Intent 调用安装程序对话框来更新自身。 The update functionality does not work anymore on Android 10.
更新功能不再适用于 Android 10。
I need to use the PackageInstaller API on Android 10 now, but i can't get it to work.我现在需要在 Android 10 上使用 PackageInstaller API,但我无法让它工作。 My app is not a device or profile owner, but since i don't want a silent install so i think it should be fine.
我的应用程序不是设备或配置文件所有者,但由于我不想进行静默安装,所以我认为应该没问题。
My problem is that as soon as i commit the session absolutely nothing happens.我的问题是,一旦我提交会话,绝对不会发生任何事情。
PackageInstaller installer = activity.PackageManager.PackageInstaller;
PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
int sessionId = installer.CreateSession(sessionParams);
PackageInstaller.Session session = installer.OpenSession(sessionId);
var input = new FileStream(pfad, FileMode.Open, FileAccess.Read);
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();
Intent intent = new Intent(activity, activity.Class);
intent.SetAction("com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED");
PendingIntent pendingIntent = PendingIntent.GetActivity(activity, 0, intent, 0);
IntentSender statusReceiver = pendingIntent.IntentSender;
// Commit the session (this will start the installation workflow).
session.Commit(statusReceiver);
I took a look at the DDMS and got nothing relevant out of it.我查看了 DDMS,并没有从中得到任何相关信息。 One thing that might be of interest is that when i
Dispose()
the streams, i get an IOException: write failed (EBADF) bad file descriptor
which would indicate a bad APK.可能感兴趣的一件事是,当我
Dispose()
流时,我收到IOException: write failed (EBADF) bad file descriptor
,这表明 APK 有问题。 But i doubt it's that because i can install the APK using a file manager without a hitch.但我怀疑这是因为我可以使用文件管理器顺利安装 APK。 Googling the error didn't lead me anywhere.
谷歌搜索错误并没有把我带到任何地方。
How can i fix this problem?我该如何解决这个问题?
There are a couple of things you need to make sure in order for the apk installation to succeed in Android Q:为了在 Android Q 中成功安装 apk,您需要确保以下几点:
private static void AddApkToInstallSession(Context context, Android.Net.Uri apkUri, PackageInstaller.Session session)
{
var packageInSession = session.OpenWrite("package", 0, -1);
var input = context.ContentResolver.OpenInputStream(apkUri);
try
{
if (input != null)
{
input.CopyTo(packageInSession);
}
else
{
throw new Exception("Inputstream is null");
}
}
finally
{
packageInSession.Close();
input.Close();
}
//That this is necessary could be a Xamarin bug.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
protected override void OnNewIntent(Intent intent)
{
Bundle extras = intent.Extras;
if (PACKAGE_INSTALLED_ACTION.Equals(intent.Action))
{
var status = extras.GetInt(PackageInstaller.ExtraStatus);
var message = extras.GetString(PackageInstaller.ExtraStatusMessage);
switch (status)
{
case (int)PackageInstallStatus.PendingUserAction:
// Ask user to confirm the installation
var confirmIntent = (Intent)extras.Get(Intent.ExtraIntent);
StartActivity(confirmIntent);
break;
case (int)PackageInstallStatus.Success:
//TODO: Handle success
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:
//TODO: Handle failures
break;
}
}
}
LaunchMode.SingleTop
LaunchMode.SingleTop
PackageManager.CanRequestPackageInstalls()
.PackageManager.CanRequestPackageInstalls()
来检查是否是这种情况。 If this function returns false, you can open the application options window by using this code:StartActivity(new Intent(
Android.Provider.Settings.ActionManageUnknownAppSources,
Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));
so the user can easily set the switch to enable this.因此用户可以轻松设置开关以启用此功能。
public void InstallPackageAndroidQAndAbove(Android.Net.Uri apkUri)
{
var packageInstaller = PackageManager.PackageInstaller;
var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
int sessionId = packageInstaller.CreateSession(sessionParams);
var session = packageInstaller.OpenSession(sessionId);
AddApkToInstallSession(apkUri, session);
// Create an install status receiver.
var intent = new Intent(this, this.Class);
intent.SetAction(PACKAGE_INSTALLED_ACTION);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
var statusReceiver = pendingIntent.IntentSender;
// Commit the session (this will start the installation workflow).
session.Commit(statusReceiver);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.