最新版本 android 10
,我通过下载更新了应用程序 DownloadManager
并通过 Intent
通过运行它 StartActivity
. 但是在 Android 10
,这不再有效。我发现只有一个例子 PackageManager
但不幸的是什么都不管用。在输出端 OnNewIntent
我总是得到结果- PackageInstallStatus.FailureInvalid
. 该示例经过重新设计,以便从请求的http流记录到会话中,并从文件记录到原始会话中。但这没有问题,我试过从文件。它还能用吗?也许有一些特点?我还获得安装软件包的权限。
void WebUpdateApk()
{
Action update = async () =>
{
if (PackageManager.CanRequestPackageInstalls() == true)
{
var packageInstaller = PackageManager.PackageInstaller;
var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
int sessionId = packageInstaller.CreateSession(sessionParams);
var session = packageInstaller.OpenSession(sessionId);
try
{
var url = "http://mywebsite.com/getapk.php?apk=myapp.apk";
var httpResponse = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseContentRead);
try
{
if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
{
Stream input = await httpResponse.Content.ReadAsStreamAsync();
try
{
var packageInSession = session.OpenWrite("package", 0, -1);
try
{
if (input != null)
{
input.CopyTo(packageInSession);
}
else
{
throw new Exception("Inputstream is null");
}
}
finally
{
packageInSession.Close();
}
}
finally
{
input.Close();
}
}
}
catch { }
finally
{
httpResponse.Dispose();
}
}
catch { }
Intent intent = new Intent(Application.Context, Class);
intent.SetAction("SESSION_API_PACKAGE_INSTALLED");
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
IntentSender statusReceiver = pendingIntent.IntentSender;
session.Commit(statusReceiver);
}
else
{
Toast.MakeText(this, "No Package Installer Permission", ToastLength.Long).Show();
}
};
if (PackageManager.CanRequestPackageInstalls() == false)
{
RestartActivity = update;
StartActivity(new Intent(Android.Provider.Settings.ActionManageUnknownAppSources,
Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));
}
else update();
}
protected override void OnNewIntent(Intent intent)
{
Bundle extras = intent.Extras;
if ("SESSION_API_PACKAGE_INSTALLED".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;
}
Toast.MakeText(this, "OnNewIntent status = " + status.ToString(), ToastLength.Long).Show();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!