美文网首页
apk里安装apk

apk里安装apk

作者: ccsosnfs | 来源:发表于2019-03-08 16:50 被阅读0次

    假设在A apk中放入 B apk,在A apk安装运行后,要安装 B apk

    将B apk放在raw目录。

    将B apk拷贝至 /data/data/A apk的包名/files

    设置B apk的权限。

    通过系统安装器安装。

    Java代码  String apkPath = "/data/data/" + getPackageName() + "/files";  
    String apkName = "b.apk";  
    File file = new File(apkPath,apkName);  
      
    try  
    {  
                    InputStream is = getResources().openRawResource(R.raw.b);  
      
                    if(!file.exists())  
                    {  
                        file.createNewFile();  
      
                        FileOutputStream os = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);  
      
                        byte[] bytes = new byte[512];  
                        int i = -1;  
                        while((i = is.read(bytes))>0)  
                        {  
                            os.write(bytes);  
                        }  
      
                        os.close();  
                        is.close();  
                        Log.d(LOG_TAG, apkName + " has been copy to " + apkPath);  
                    };  
      
                    String permission="666";  
      
                    try  
                    {  
                        String command = "chmod " + permission + " " + apkPath + "/" + apkName;  
                        Runtime runtime = Runtime.getRuntime();  
                        runtime.exec(command);  
                    }  
                    catch (IOException e)  
                    {  
                        e.printStackTrace();  
                    }  
      
                }  
                catch(Exception e)  
                {  
                    Log.d(LOG_TAG, e.toString());  
                    finish();  
                }  
      
                Intent intent = new Intent();  
                intent.setAction(android.content.Intent.ACTION_VIEW);  
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");  
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                startActivity(intent);  
    
    

    如果apk文件过大,如下

    http://www.hfdigg.com/SrcShow.asp?Src_ID=10092

    android raw文件夹下.db后缀文件大于1M时,拷贝时将会出现:DEBUG/asset(725): Data exceeds UNCOMPRESS_DATA_MAX (1662976 vs 1048576)

    出现这个问题的原因是,assetsManager 无法处理大于1M的文件的压缩和解压。
    但以下文件类型,因为是已经压缩过的,不会进行压缩处理,如下:
    /* these formats are already compressed, or dont compress well /
    static const char
    kNoCompressExt[] = {
    ".jpg", ".jpeg", ".png", ".gif",
    ".wav", ".mp2", ".mp3", ".ogg", ".aac",
    ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
    ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
    ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
    ".amr", ".awb", ".wma", ".wmv"
    };
    【解决办法】将Sqlite db文件,先改名为.jpg文件,放在assets中,然后在程序第一次启动时,改名拷贝到/data/data

    相关文章

      网友评论

          本文标题:apk里安装apk

          本文链接:https://www.haomeiwen.com/subject/xqtdpqtx.html