简体   繁体   English

Xamarin.Android PackageInstaller Session.commit apk静默安装上的“文件仍打开”异常

[英]Xamarin.Android PackageInstaller Session.commit “Files still open” Exception on apk silent install

I'm trying to code a proof of concept, with Xamarin android . 我正在尝试使用Xamarin android编写概念证明。 A sort of EMM tool, ie an application which will be on charge to install other application and manage the device. 一种EMM工具,即一个负责安装其他应用程序和管理设备的应用程序。 So Android Marshmallow is a good start with android for work features. 因此, Android Marshmallow是android具有良好工作功能的良好开端。

My app is Device Owner therefore it should have no problem to silently install other applications. 我的应用程序是设备所有者,因此以静默方式安装其他应用程序应该没有问题。 It can download an apk from a website without a problem. 它可以毫无问题地从网站下载apk。 But when I try to install it, it throws an " Files still open " exception despite of calling all Close() methods. 但是,当我尝试安装它时,尽管调用了所有Close()方法,它仍会引发“ 文件仍处于打开状态 ”异常。

I have taken my code from the example of the excellent android-testdpc sample on github here 我把我的代码出色的Android testdpc样品的例子在github 这里

I have changed it for it work in c# with Xamarin. 我已经更改了它,因为它可以在Xamarin的c#中工作。

Here is my code : 这是我的代码:

    public static bool InstallPackage(Context context, Handler handler, InputStream input, String packageName)
    {
        try
        {
            PackageInstaller packageInstaller = context.PackageManager.PackageInstaller;
            PackageInstaller.SessionParams param = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
            param.SetAppPackageName(packageName);
            // set params
            int sessionId = packageInstaller.CreateSession(param);
            PackageInstaller.Session session = packageInstaller.OpenSession(sessionId);
            using (System.IO.Stream output = session.OpenWrite("COSU", 0, -1))
            {
                byte[] buffer = new byte[65536];
                int c;
                while ((c = input.Read(buffer)) != -1)
                {
                    output.Write(buffer, 0, c);
                }
                session.Fsync(output);
                input.Close();
                output.Close();
            }
            session.Commit(createIntentSender(context, sessionId)); // this line throws exception 'Files stil open'
            return true;
        }
        catch (Exception ex)
        {
            Log.Error(TAG, "Error installing package: " + packageName, ex);
            handler.SendMessage(handler.ObtainMessage(Common.MSG_INSTALL_FAIL,
                    packageName));
            return false;
        }
    }

I'm stuck with this for the moment. 我暂时坚持这一点。 if I have the time I will try to install android studio and test my code in java to see if the problem come from Xamarin. 如果有时间,我将尝试安装android studio并在Java中测试我的代码,以查看问题是否来自Xamarin。

If someone got any clue for my problem I will really appreciate the help 如果有人对我的问题有任何了解,我将非常感谢您的帮助

SecurityException : if streams opened through openWrite(String, long, long) are still open. SecurityException:如果通过openWrite(String,long,long)打开的流仍处于打开状态。

The Java peer object is not closed yet, this is how I force it for the PackageInstaller.Session.Commit : Java对等对象尚未关闭,这就是我如何对PackageInstaller.Session.Commit 强制

var input = Assets.Open(packageName);
var packageInstaller = PackageManager.PackageInstaller;
var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
sessionParams.SetAppPackageName(packageName);
int sessionId = packageInstaller.CreateSession(sessionParams);
var session = packageInstaller.OpenSession(sessionId);
using (var output = session.OpenWrite(packageName, 0, -1))
{
    input.CopyTo(output);
    session.Fsync(output);
    foreach (var name in session.GetNames())
        Log.Debug("Installer", name);
    output.Close();
    output.Dispose();
    input.Close();
    input.Dispose();
    GC.Collect();
}
var pendingIntent = PendingIntent.GetBroadcast(BaseContext, sessionId, new Intent(Intent.ActionInstallPackage), 0);
session.Commit(pendingIntent.IntentSender);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM