java Android将自定义内容分享到不同的应用程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android将自定义内容分享到不同的应用程相关的知识,希望对你有一定的参考价值。

 /**
   * http://stackoverflow.com/questions/9730243/how-to-filter-specific-apps-for-action-send-intent-and-set-a-different-text-for?answertab=votes#tab-top
   */
  public void onShareClick() {
    Resources resources = getResources();
    PackageManager pm = getPackageManager();

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);

    List<LabeledIntent> intentList = new ArrayList<>();

    for (int i = 0; i < resInfo.size(); i++) {
      // Extract the label, append it, and repackage it in a LabeledIntent
      ResolveInfo ri = resInfo.get(i);
      String packageName = ri.activityInfo.packageName;

      Intent intent = new Intent();
      intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
      intent.setAction(Intent.ACTION_SEND);

      Log.d(">>>>", packageName);

      if (packageName.contains("email") // Email applicaitons
          || packageName.endsWith(".gm") // Gmail
          ) {
        // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
        intent.putExtra(Intent.EXTRA_TEXT, "http://google.com/\n\nArticle content, Article content, Article content, Article content, Article content, Article content, Article content, Article content, Article content, Article content");
        intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
        intent.setType("message/rfc822");
      } else {
        intent.putExtra(Intent.EXTRA_TEXT, "http://google.com/");
        intent.setType("text/plain");
      }

      intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
    }

    // convert intentList to array
    Intent openInChooser = Intent.createChooser(intentList.get(0), "Choose action");
    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new LabeledIntent[intentList.size()]));
    startActivity(openInChooser);
  }

以上是关于java Android将自定义内容分享到不同的应用程的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义输入法的偏好添加到 Android 设置应用程序?

Android Gradle 插件将自定义 Gradle 插件上传到自建 Maven 仓库 ② ( java 和 groovy 插件自带文档任务 | 自定义文档打包任务 | 生成文档包 )

Android Gradle 插件将自定义 Gradle 插件上传到远程仓库中 ① ( 在 Module 模块中自定义 Gradle 插件 | 创建自定义插件类型模块 | 手动导入相关依赖 )

android以编程方式将自定义按钮添加到布局

将自定义项目添加到 Android 共享表?

将自定义视图附加到 Android 键盘的顶部