【问题标题】:How to share my own app apk to other using cordova如何使用cordova将我自己的应用apk分享给其他人
【发布时间】:2018-03-12 16:28:38
【问题描述】:

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

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

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

   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 扩展 java 文件调用该函数(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扩展类调用函数时,它显示以下错误。

【问题讨论】:

    标签: android cordova apk phonegap


    【解决方案1】:

    我在以下 URL 中找到了该插件,这可以生成您的应用程序 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
    

    【讨论】:

      【解决方案2】:

      你应该尝试换行

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

      【讨论】:

      • 它显示“未处理的异常:android.content.pm.PackageManager.NameNotFoundException”错误
      • 您应该确认 ("package_name",0) 中提供的应用程序的包名称;
      • 更多知识可以参考Here
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2019-10-02
      • 1970-01-01
      相关资源
      最近更新 更多