首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发出用.apk安装PackageInstaller

发出用.apk安装PackageInstaller
EN

Stack Overflow用户
提问于 2022-03-13 09:18:32
回答 1查看 339关注 0票数 1

我的目标是安装一个存储在设备上的.apk文件。我试过使用ACTION_VIEW意图,但我认为它在安卓10中已经被废弃了。

我已经浏览了一半的网页,但大多数答案要么是使用废弃的意图,kotlin,要么就是不起作用。

这是我的密码

代码语言:javascript
复制
 private void installApk() throws IOException {
        if (!requireContext().getPackageManager().canRequestPackageInstalls()) {
            startActivity(new Intent(
                    Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
                    Uri.parse("package:" + requireContext().getPackageName())));
        } else {

//            Log.d(TAG, "File Exists?: " + outputFile.exists() + " ; " + outputFile.getAbsolutePath() + " ; " + "can read?" + outputFile.canRead() + " ; " + "can write?" + outputFile.canWrite());
            PackageInstaller packageInstaller = requireContext().getPackageManager().getPackageInstaller();
            PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller
                    .SessionParams.MODE_FULL_INSTALL);
            int sessionId = packageInstaller.createSession(params);
            PackageInstaller.Session session = packageInstaller.openSession(sessionId);

                addApkToInstallSession(session);

                Intent callbackIntent = new Intent(requireContext(), APKInstallService.class);

                PendingIntent pendingIntent = PendingIntent.getService(requireContext(), 0, callbackIntent, 0);
                // This is sending the result of the installation to the APKInstallService.
                IntentSender statusReceiver = pendingIntent.getIntentSender();
                session.commit(statusReceiver);


        }
    }

这是我在onClickListener中调用的方法

代码语言:javascript
复制
  private void addApkToInstallSession(PackageInstaller.Session session)
            throws IOException {
        try (OutputStream packageInSession = session.openWrite("package", 0, -1);
             InputStream is = requireContext().getContentResolver().openInputStream(fileURI)) {
            byte[] buffer = new byte[16384];
            int n;
            while ((n = is.read(buffer)) >= 0) {
                packageInSession.write(buffer, 0, n);
            }
        }
    }

将文件添加到会话中。

代码语言:javascript
复制
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class APKInstallService extends Service {
    private static final String TAG = "APKInstallService";


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -999);

        switch (status) {
            case PackageInstaller.STATUS_PENDING_USER_ACTION:
                Log.d(TAG, "Requesting user confirmation for installation");
                // This is a way to get the intent that was passed to the service.
                //Intent confirmationIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
                //confirmationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //try {
                  //  startActivity(confirmationIntent);
                //} catch (Exception e) {
                    //No action

//                }
                break;
            case PackageInstaller.STATUS_SUCCESS:
                Log.d(TAG, "Installation succeed");
                break;
            default:
                Log.d(TAG, "Installation failed");
                break;
        }
        stopSelf();
        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

得到结果

我对编码很陌生,如果这是个愚蠢的问题,我很抱歉

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-16 15:46:55

看起来问题在于,我的模拟器没有安装gapps,尽管在任何地方都没有声明,但显然是这种操作的依赖。虽然我会推荐CommonsWare在他的评论中提到的章节,因为它们比稀少的文档要全面得多。

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

https://stackoverflow.com/questions/71455373

复制
相关文章

相似问题

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