Ejemplo n.º 1
0
  /**
   * 发送分享
   *
   * @param mContext app上下文
   * @param shareContentMeta 分享内容元数据
   * @param isMultiple 是否同时发送多种数据
   * @param shareEntry 分享入口,目前有网页文本、网页图片、屏幕截图、书签、历史、下载管理几个入口
   * @param isCheckPriority 是否需要检查分享优先级
   * @return 是否分享成功
   */
  public static boolean sendShare(
      Context mContext,
      BdSharerOM shareContentMeta,
      boolean isMultiple,
      int shareEntry,
      boolean isCheckPriority) {

    if (shareContentMeta == null || shareContentMeta.getContent() == null) {
      BdLog.e("shareContentMeta is null.");
      return false;
    }

    String action = "android.intent.action.SEND";
    Intent intent = new Intent(action);
    // 不管有没有附件,都设置文本信息,第三方软件图片分享可能会不支持android.intent.extra.TEXT
    // 如果有文字,则分享文字
    intent.putExtra("android.intent.extra.TEXT", shareContentMeta.getContent());
    BdLog.d(shareContentMeta.getContent());
    int contentType = shareContentMeta.getContentType();
    if (contentType == BdSharerOM.TYPE_TEXT) {
      intent.setType("text/plain");
    }

    Intent intent2 = Intent.createChooser(intent, mContext.getString(R.string.share_channel));
    try {
      if (mContext instanceof Activity) {
        Activity act = (Activity) mContext;
        act.startActivityForResult(intent2, shareEntry);
      }
    } catch (ActivityNotFoundException e) {
      BdLog.e("share app not found.", e);
      return false;
    } catch (Exception e) {
      BdLog.e("share exception.", e);
      return false;
    }
    return true;
  }
Ejemplo n.º 2
0
  /**
   * 分享文本
   *
   * @param mContext app上下文
   * @param templateId 文本内容模版
   * @param text1 文本内容字段一
   * @param text2 文本内容字段二
   * @return 是否发送成功
   */
  public static boolean sendTextShare(
      final Context mContext, final int templateId, final String text1, final String text2) {

    BdLog.d(text1 + ", " + text2);

    final BdSharerOM shareContentMeta = new BdSharerOM();
    shareContentMeta.setContentType(BdSharerOM.TYPE_TEXT);

    waitTinyUrlDialog = new BdWaitingDialog(mContext);
    waitTinyUrlDialog.setMessage(mContext.getString(R.string.share_waiting));
    waitTinyUrlDialog.setCancelable(true);
    waitTinyUrlDialog.setOnCancelListener(
        new OnCancelListener() {
          @Override
          public void onCancel(DialogInterface dialog) {
            willCancelShare = true;
          }
        });

    waitTinyUrlDialog.show();

    willCancelShare = false;
    BdTinyUrlGenerator generator = new BdTinyUrlGenerator();
    generator.setEventListener(
        new BdTinyUrlRecievedListener() {

          @Override
          public void onTinyUrlRecieved(String tinyUrl) {
            String extra1 = text1;
            String extra2 = tinyUrl;
            if (text1 == null) {
              extra1 = "";
            }
            //				if (extra1 != "") {
            //					extra1 = text1 + TextUtil.COMMA;
            //				}
            if (tinyUrl == null) {
              extra2 = "";
            }
            //				if (extra2 != "") {
            //					extra2 = Sharer.SHARE_DETAIL_TIP + tinyUrl;
            //				}
            if (!"".equals(extra1) && "".equals(extra2)) {
              extra1 = text1;
            }
            BdLog.d(extra1 + ", " + extra2);
            String text = mContext.getString(templateId, extra1, extra2);
            shareContentMeta.setContent(text);
            shareContentMeta.setExtra1(text1);
            shareContentMeta.setExtra2(tinyUrl);
            shareContentMeta.setContent(preProcContent(mContext, shareContentMeta));
            if (!willCancelShare) {
              sendOk =
                  BdSharer.sendShare(
                      mContext, shareContentMeta, false, SHARE_RERQUEST_CODE_DEFAULT, false);
            }
            if (waitTinyUrlDialog != null) {
              waitTinyUrlDialog.cancel();
              waitTinyUrlDialog = null;
            }
          }
        });

    generator.generate(text2);

    return sendOk;
  }