如何在Android中以编程方式使用共享意图发送APK [英] How to send apk using share intent programatically in android

查看:89
本文介绍了如何在Android中以编程方式使用共享意图发送APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有某些应用程序的捆绑包名称,因此现在我需要生成所有应用程序的apk,并希望使用共享意图进行发送.

I have bundle names of some applications so now i need to generate that all application's apks and want to send using share intent.

我尝试了很多可能的解决方案,但没有找到任何方法

i tried lot of possible solutions but didn't find any approach

我使用此链接,但无法正常工作打包我的应用程序并分享给其他+ android

i use this link but didn't working packing my app and share to other + android

预先感谢

推荐答案

我认为这应该对您有帮助

I think this should help you

ArrayList<Uri> arrayListapkFilepath; // define global

                //put this code when you wants to share apk
                arrayListapkFilepath = new ArrayList<Uri>();

                shareAPK(getPackageName());
                // you can pass bundle id of installed app in your device instead of getPackageName()
                Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                intent.setType("application/vnd.android.package-archive");
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
                        arrayListapkFilepath);
                startActivity(Intent.createChooser(intent, "Share " +
                        arrayListapkFilepath.size() + " Files Via"));

//Method
public void shareAPK(String bundle_id) {
    File f1;
    File f2 = null;

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
    int z = 0;
    for (Object object : pkgAppsList) {

        ResolveInfo info = (ResolveInfo) object;
        if (info.activityInfo.packageName.equals(bundle_id)) {

            f1 = new File(info.activityInfo.applicationInfo.publicSourceDir);

            Log.v("file--",
                    " " + f1.getName().toString() + "----" + info.loadLabel(getPackageManager()));
            try {

                String file_name = info.loadLabel(getPackageManager()).toString();
                Log.d("file_name--", " " + file_name);

                f2 = new File(Environment.getExternalStorageDirectory().toString() + "/Folder");
                f2.mkdirs();
                f2 = new File(f2.getPath() + "/" + file_name + ".apk");
                f2.createNewFile();

                InputStream in = new FileInputStream(f1);

                OutputStream out = new FileOutputStream(f2);

                // byte[] buf = new byte[1024];
                byte[] buf = new byte[4096];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
                System.out.println("File copied.");
            } catch (FileNotFoundException ex) {
                System.out.println(ex.getMessage() + " in the specified directory.");
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    arrayListapkFilepath.add(Uri.fromFile(new File(f2.getAbsolutePath())));

}

这篇关于如何在Android中以编程方式使用共享意图发送APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆