首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用INSTALL_PACKAGES将APK安装为系统应用程序

使用INSTALL_PACKAGES将APK安装为系统应用程序
EN

Stack Overflow用户
提问于 2018-05-31 07:29:36
回答 1查看 2.4K关注 0票数 2

我已经开发了一个应用程序,它以系统权限运行(它内置于AOSP的映像中),并具有INSTALL_PACKAGES权限。

我的目标是在后台静默安装APK,而无需用户交互。我读过关于stackoverflow的不同方法,但它们中的大多数要么过时了,要么根本不起作用。

例如,如果我试图使用PackageManager (无论是通过反射还是通过在我的应用程序中作为外壳命令运行),我会得到以下错误,这就是我当前遇到的问题。

代码语言:javascript
复制
11-20 19:06:10.999  4965  4965 D AndroidRuntime: >>>>>> START 

com.android.internal.os.RuntimeInit uid 10039 <<<<<<
11-20 19:06:11.199  4965  4965 D AndroidRuntime: Calling main entry com.android.commands.pm.Pm
11-20 19:06:11.268  4965  4965 E Pm      : Error
11-20 19:06:11.268  4965  4965 E Pm      : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AppOpsManager.checkPackage(int, java.lang.String)' on a null object reference
11-20 19:06:11.268  4965  4965 E Pm      :  at android.os.Parcel.readException(Parcel.java:2010)
11-20 19:06:11.268  4965  4965 E Pm      :  at android.os.Parcel.readException(Parcel.java:1950)
11-20 19:06:11.268  4965  4965 E Pm      :  at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:254)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.doCreateSession(Pm.java:607)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.runInstall(Pm.java:431)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.run(Pm.java:150)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.commands.pm.Pm.main(Pm.java:107)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
11-20 19:06:11.268  4965  4965 E Pm      :  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)
11-20 19:06:11.270  4965  4965 I app_process: System.exit called, status: 1
11-20 19:06:11.270  4965  4965 I AndroidRuntime: VM exiting with result code 1.

谁能告诉我为什么会发生这个错误,或者是其他解决方案,或者是用INSTALL_PACKAGES静默安装APKS的“现代”方式?

感谢您的帮助。

编辑:

对于将其标记为空指针异常的副本的人,您如何创建将是副本的连接?我特别询问它背后的原因,而不是关于空指针异常本身。你重复的建议完全没有意义。

EDIT 2:

我尝试安装APK的代码(从AOSP源代码的安装包应用程序复制):

代码语言:javascript
复制
public boolean install(Context context) {

    Log.d(TAG, "Start Installation");

    String packageName = "com.spotify.music";
    String apkPath = Environment.getExternalStorageDirectory().toString() + "/APKs/spotify.apk";

    PackageInstaller.Session session;
    int sessionId;
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    try {
        sessionId = context.getPackageManager().getPackageInstaller().createSession(params);
        session = context.getPackageManager().getPackageInstaller().openSession(sessionId);
    } catch (IOException e) {
        Log.w(TAG, "Error creating/opening the installing session", e);
        return false;
    }

    try {
        File file = new File(apkPath);

        try (InputStream in = new FileInputStream(file)) {
            long sizeBytes = file.length();
            try (OutputStream out = session.openWrite("PackageInstaller", 0, sizeBytes)) {
                byte[] buffer = new byte[1024 * 1024];
                while (true) {

                    int numRead = in.read(buffer);

                    if (numRead == -1) {
                        session.fsync(out);
                        break;
                    }

                    out.write(buffer, 0, numRead);
                    if (sizeBytes > 0) {
                        float fraction = ((float) numRead / (float) sizeBytes);
                        Log.d(TAG, "Install progress: " + fraction);
                    }
                }
            }
        }

        Log.d(TAG, "Install finished");
        session.commit(PendingIntent.getBroadcast(context, sessionId, new Intent("android.intent.action.MAIN"), 0).getIntentSender());

        return true;
    } catch (IOException | SecurityException e) {
        Log.e(TAG, "Could not write package", e);

        session.close();

        return false;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-05 04:24:55

我们使用以下代码:

代码语言:javascript
复制
Uri apkUri = Uri.fromFile(apkFile);
IPackageInstallObserver observer = null;
String installerPackageName = "MyInstaller";
getPackageManager().installPackage(apkUri, observer, PackageManager.INSTALL_REPLACE_EXISTING, installerPackageName);

它已被弃用,但即使在Android 8.1上也能正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50614646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档