Android apk的安装

Posted Dr_abandon新秀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android apk的安装相关的知识,希望对你有一定的参考价值。

1 。已经root的设备 可以实现静默安装,调用pm install -r来安装的,-r是保留原来APP的数据

  private   boolean slienceInstall(String apkUrl) 

        boolean result =false;

        DataOutputStream dataOutputStream =null;

        BufferedReader errorStream =null;

        try 

            Process process =Runtime.getRuntime().exec("su");

            dataOutputStream =new DataOutputStream(process.getOutputStream());

            String command ="pm install -r " + apkUrl +"\\n";

            dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));

            dataOutputStream.flush();

            dataOutputStream.writeBytes("exit\\n");

            dataOutputStream.flush();

            process.waitFor();

            errorStream =new BufferedReader(new InputStreamReader(process.getErrorStream()));

            String msg ="";

            String line;

            while ((line = errorStream.readLine()) !=null) 

                msg += line;

            

            if (!msg.contains("Failure")) 

                result =true;

            

        catch (Exception e) 

        finally 

            try 

                if (dataOutputStream !=null) 

                    dataOutputStream.close();

                

                if (errorStream !=null) 

                    errorStream.close();

                

            catch (IOException e) 

            

        

        return result;

    

2.需要系统签名的方式,才能实现静默安装

①权限

 <uses-permission android:name= "android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" tools:ignore="ProtectedPermissions" />
private boolean slienceInstallWithSysSign(String filePath) 

        File file =new File(filePath);

        PackageManager packageManager =getPackageManager();
        Class pmClz =packageManager.getClass();
        try 

            if (Build.VERSION.SDK_INT >=21) 

                Class aClass =Class.forName("android.app.PackageInstallObserver");

                Constructor constructor =aClass.getDeclaredConstructor();

                constructor.setAccessible(true);

                Object installObserver =constructor.newInstance();

                Method method =pmClz.getDeclaredMethod("installPackage",Uri.class,aClass,int.class,String.class);

                method.setAccessible(true);

                method.invoke(packageManager,Uri.fromFile(file),installObserver,2,null);

            else 

                Method method =pmClz.getDeclaredMethod("installPackage",

                        Uri.class,Class.forName("android.content.pm.IPackageInstallObserver"),int.class,String.class);

                method.setAccessible(true);

                method.invoke(packageManager,Uri.fromFile(file),null,2,null);

            

            return true;

        catch (Exception e) 

            e.printStackTrace();

        

        return false;

    

3.需要手动点击确定才能安装

    /**
     * 7.0以上直接安装
     *需要点击确定
     * @param context
     * @param path
     */
    public void start7Install(Context context, String path) 

        Uri apkUri = FileProvider.getUriForFile(context, getPackageName() + ".provider", new File(path));//在AndroidManifest中的添加provider

        Intent install = new Intent(Intent.ACTION_VIEW);

        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        install.setDataAndType(apkUri, "application/vnd.android.package-archive");

        startActivity(install);

    

添加provider

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="$applicationId.provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />

        </provider>

在res里添加xml文件夹,添加provider_paths

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
</paths>

以上是关于Android apk的安装的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android上安装apk软件

Android APK安装过程学习笔记

如何使用ionic打包android的apk

Android Studio Terminal(命令)安装apk

Android 安装包优化APK 打包流程 ( 文件结构 | 打包流程 | 安装流程 | 安卓虚拟机 )

安卓Android软件安装包apk是啥文件格式