如何使用 cordova 将我自己的应用程序 apk 分享给其他人

How to share my own app apk to other using cordova

我想在 cordova 应用程序中与其他人共享我自己的 apk 文件。我尝试了很多插件,但所有的插件都只是用来分享应用名称和一些描述而已。

https://www.npmjs.com/package/cordova-plugin-share https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin

所以我决定创建自己的插件并且我搜索了 java 的共享 apk 代码,我得到了以下代码,当我从 调用该函数时它工作正常MainActivity.java

   private void shareApplication() {
    ApplicationInfo app = getApplicationContext().getApplicationInfo();
    String filePath = app.sourceDir;

    Intent intent = new Intent(Intent.ACTION_SEND);

    // MIME of .apk is "application/vnd.android.package-archive".
    // but Bluetooth does not accept this. Let's use "*/*" instead.
    intent.setType("*/*");

    // Append file and send Intent
    File originalApk = new File(filePath);

    try {
        //Make new directory in new location
        File tempFile = new File(getExternalCacheDir() + "/ExtractedApk");
        //If directory doesn't exists create new
        if (!tempFile.isDirectory())
            if (!tempFile.mkdirs())
                return;
        //Get application's name and convert to lowercase
        tempFile = new File(tempFile.getPath() + "/" + getString(app.labelRes).replace(" ","").toLowerCase() + ".apk");
        //If file doesn't exists create new
        if (!tempFile.exists()) {
            if (!tempFile.createNewFile()) {
                return;
            }
        }
        //Copy file to new location
        InputStream in = new FileInputStream(originalApk);
        OutputStream out = new FileOutputStream(tempFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
        //Open share dialog
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
        startActivity(Intent.createChooser(intent, "Share app via"));

    } catch (IOException e) {
        e.printStackTrace();
    }
}

但我想从 cordova extended java file.

调用该函数 (shareApplication())
public class AppVersion extends CordovaPlugin {
  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {
      if (action.equals("shareApk")) {
            MainActivity cc=new MainActivity();
            cc.shareApplication();
            MyClass myClass = new MyClass(c);
      }
      return false;
    } catch (NameNotFoundException e) {
      callbackContext.success("N/A");
      return true;
    }
}

但是当我从 cordova extended class 调用函数时,它显示以下错误。

你应该尝试更改行

ApplicationInfo app = getApplicationContext().getApplicationInfo();

ApplicationInfo app = this.getPackageManager().getApplicationInfo("package_name", 0);

我在以下网址中找到了该插件,它可以生成您的应用程序 apk 文件,并最终弹出可用的具有共享功能的应用程序。然后您可以选择应用程序并共享它。

https://github.com/merbin2012/cordova-plugin-codeplay-share-own-apk https://www.npmjs.com/package/cordova-plugin-codeplay-share-own-apk

cordova plugin add cordova-plugin-codeplay-share-own-apk