封装一个类,调用系统的的方法进行静默安装

public class PackageManagerCompatP {

    private static final String TAG = PackageManagerCompatP.class.getSimpleName();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void install(Context context, String apkFilePath, PackageManager packageManager) {
        File apkFile = new File(apkFilePath);
        PackageInstaller packageInstaller = packageManager.getPackageInstaller();
        PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        sessionParams.setSize(apkFile.length());

        int sessionId = createSession(packageInstaller, sessionParams);
        if (sessionId != -1) {
            boolean copySuccess = copyInstallFile(packageInstaller, sessionId, apkFilePath);
            if (copySuccess) {
                execInstallCommand(context, packageInstaller, sessionId);
            }
        }
    }



    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static int createSession(PackageInstaller packageInstaller,
                              PackageInstaller.SessionParams sessionParams) {
        int sessionId = -1;
        try {
            sessionId = packageInstaller.createSession(sessionParams);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sessionId;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static boolean copyInstallFile(PackageInstaller packageInstaller,
                                    int sessionId, String apkFilePath) {
        InputStream in = null;
        OutputStream out = null;
        PackageInstaller.Session session = null;
        boolean success = false;
        try {
            File apkFile = new File(apkFilePath);
            session = packageInstaller.openSession(sessionId);
            out = session.openWrite("base.apk", 0, apkFile.length());
            in = new FileInputStream(apkFile);
            int total = 0, c;
            byte[] buffer = new byte[65536];
            while ((c = in.read(buffer)) != -1) {
                total += c;
                out.write(buffer, 0, c);
            }
            session.fsync(out);
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(out);
            closeQuietly(in);
            closeQuietly(session);
        }
        return success;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId) {
        PackageInstaller.Session session = null;
        try {
            session = packageInstaller.openSession(sessionId);
            Intent intent = new Intent(context, InstallResultReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            session.commit(pendingIntent.getIntentSender());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(session);
        }
    }
    private static void closeQuietly(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException ignored) {
                ignored.printStackTrace();
            }
        }
    }
}

调用的话很简单

PackageManagerCompatP.install(getApplicationContext(),"sdcard/test.apk",getPackageManager());

需要权限

  <uses-permission android:name="android.permission.INSTALL_PACKAGES" />

测试了一下,可以不用添加

android:sharedUserId="android.uid.system"

为了让app有权限,可以让app进行系统签名,或者可以修改PermissionManagerService.java文件

                 if (DEBUG_PERMISSIONS) {
                     Slog.i(TAG, "Granting permission " + perm + " to package " + pkg.packageName);
                 }
+				
+				//add by jueme for apk permission at 20200915
+				if(pkg.packageName.equals("com.jueme.android.autoinstall")){
+					Log.i(TAG, "com.jueme.android.autoinstall");
+					grant = GRANT_INSTALL;
+				}
+				//add end 
 
+
                 if (grant != GRANT_DENIED) {
                     if (!ps.isSystem() && ps.areInstallPermissionsFixed()) {

grant = GRANT_INSTALL给app所有的权限,只要app申请了任何的权限都默认打开。

还有一个问题就是客户要求安装之后要自启动app,可以使用广播来监听

public class InstallResultReceiver extends BroadcastReceiver {
    private static final String TAG = "InstallResultReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: "+intent.getIntExtra(PackageInstaller.EXTRA_STATUS,PackageInstaller.STATUS_FAILURE));
        if (intent != null) {
            final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,PackageInstaller.STATUS_FAILURE);
            if (status == PackageInstaller.STATUS_SUCCESS) {

            } else {

            }
        }
    }
}
 <receiver android:name=".InstallResultReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.content.pm.extra.STATUS"/>
            </intent-filter>
        </receiver>

监听其他app安装是没有问题的,可以收到广播。但是当进行自身安装的会监听不到广播,因为app安装完成之后就退出去了,这时想要在app里面监听是实现不了的。想了一个办法,把framework层修改了,找到发广播的地方PackageInstallerService.java。

static class PackageInstallObserverAdapter extends PackageInstallObserver {
    ...
    @Override
        public void onPackageInstalled(String basePackageName, int returnCode, String msg,
                Bundle extras) {
            if (PackageManager.INSTALL_SUCCEEDED == returnCode && mShowNotification) {
                boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING);
                Notification notification = buildSuccessNotification(mContext,
                        mContext.getResources()
                                .getString(update ? R.string.package_updated_device_owner :
                                        R.string.package_installed_device_owner),
                        basePackageName,
                        mUserId);
                if (notification != null) {
                    NotificationManager notificationManager = (NotificationManager)
                            mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(basePackageName,
                            SystemMessage.NOTE_PACKAGE_STATE,
                            notification);
                }
            }
			//add by jueme for start customer app at 20200927
			if(basePackageName.equals("com.jueme.android.autoinstall")){
				new Thread(new Runnable(){
					@Override
					public void run(){
						try {
							Slog.d(TAG,"onPackageInstalled basePackageName "+basePackageName+" returnCode "+returnCode+" msg "+msg);
							Thread.sleep(500);
							Intent intent = new Intent();
									intent.setClassName("com.jueme.android.autoinstall","com.jueme.android.autoinstall.MainActivity");
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							mContext.startActivity(intent);
						} catch (Exception e) {
							Slog.e(TAG,"onPackageInstalled Exception "+e.toString());
						}
					}
				}).start();
			}
			//add end
			
            final Intent fillIn = new Intent();
            fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName);
            fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, mSessionId);
            fillIn.putExtra(PackageInstaller.EXTRA_STATUS,
                    PackageManager.installStatusToPublicStatus(returnCode));
            fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE,
                    PackageManager.installStatusToString(returnCode, msg));
            fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode);
            if (extras != null) {
                final String existing = extras.getString(
                        PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE);
                if (!TextUtils.isEmpty(existing)) {
                    fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing);
                }
            }
            try {
                mTarget.sendIntent(mContext, 0, fillIn, null, null);
            } catch (SendIntentException ignored) {
            }
        }
        ...
}

这里睡500ms是因为在测试过程中发现会概率性启动不了app,原因就是在还没有完全安装好就调用了启动的方法,加了延时之后就没发现问题了。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐