Android 获取已安装的APP并提取源文件(.apk安装包)

最近项目加需求,需要获取设备内安装的所有App,并提取指定的APP的源文件(.apk安装包).于是写了个Demo,包括指定包名APP的.apk文件提取以及所有已安装的APP的.apk文件提取.

话不多说直接上代码,需要的直接带走.

/**
 * 获取已安装的APP
 * 根据应用包名获取APP的apk路径并拷贝到新建的文件夹下
 */
public class MainActivity extends AppCompatActivity {

    private static final String BACKUP_PATH = "/sdcard/backup1/";
    private static final String APK = ".apk";
    private PackageManager pm;
    private List<ResolveInfo> mApps = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);

        //Copy所有已安装APP的.apk文件到BACKUP_PATH目录下
        queryApps();
        //Copy指定包名APP的.apk文件到BACKUP_PATH目录下
        copyApk("QQ",getApk("com.tencent.mobileqq"));
    }

    //查询已安装的APP
    private void queryApps() {
        pm = this.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        if (resolveInfos != null && resolveInfos.size() > 0) {
            for (int i = 0; i < resolveInfos.size(); i++) {
                mApps.add(resolveInfos.get(i));
            }
        }
        if (mApps.size() > 0 && mApps != null) {
            for (int i = 0; i < mApps.size(); i++) {
                getApk(mApps.get(i).activityInfo.packageName);
            }
        }
    }

    private void copyApk(String name,String path){
        String dest = BACKUP_PATH + name + APK;
        //path:app程序源文件路径  dest:新的存储路径  name:app名称
        new Thread(new CopyRunnable(path, dest, name)).start();
    }
    
    private String getApk(String packageName) {
        String appDir = null;
        try {
            //通过包名获取程序源文件路径
            appDir = getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appDir;
    }


    /**
     * 将程序源文件Copy到指定目录
     */
    private class CopyRunnable implements Runnable {
        private String source;
        private String dest;
        private String key;

        public CopyRunnable(String source, String dest, String key) {
            this.source = source;
            this.dest = dest;
            this.key = key;
        }
        @SuppressLint("StringFormatInvalid")
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                int length = 1024 * 1024;
                if (!new File(BACKUP_PATH).exists()) {
                    boolean mk = new File(BACKUP_PATH).mkdirs();
                    if(mk){
                        System.out.println("true");
                    }
                }

                File fDest = new File(dest);
                if (fDest.exists()) {
                    fDest.delete();
                }
                fDest.createNewFile();
                FileInputStream in = new FileInputStream(new File(source));
                FileOutputStream out = new FileOutputStream(fDest);
                FileChannel inC = in.getChannel();
                FileChannel outC = out.getChannel();
                int i = 0;
                while (true) {
                    if (inC.position() == inC.size()) {
                        inC.close();
                        outC.close();
                        //成功
                        break;
                    }
                    if ((inC.size() - inC.position()) < 1024 * 1024) {
                        length = (int) (inC.size() - inC.position());
                    } else {
                        length = 1024 * 1024;
                    }
                    inC.transferTo(inC.position(), length, outC);
                    inC.position(inC.position() + length);
                    i++;
                }
            } catch (Exception e) {
                // TODO: handle exception
                Log.e("TAG", e.toString());
            }
        }
    }
}

 

  • 11
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值