Exemplo n.º 1
0
  @Override
  protected void init() throws Exception {
    if (mIWXAPI == null) {
      mIWXAPI = WXAPIFactory.createWXAPI(getContext(), mAppId, true);
      if (mIWXAPI.isWXAppInstalled()) {
        mIWXAPI.registerApp(mAppId);
      }
    }

    if (!mIWXAPI.isWXAppInstalled()) {
      String msg = getContext().getString(R.string.bili_share_sdk_not_install_wechat);
      Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
      throw new ShareException(msg, BiliShareStatusCode.ST_CODE_SHARE_ERROR_NOT_INSTALL);
    }
  }
Exemplo n.º 2
0
  public boolean isUsableWX(Context context) {
    boolean isUsable = true;
    api = WXAPIFactory.createWXAPI(context, APP_WX_ID);
    api.registerApp(APP_WX_ID);
    if (api.isWXAppInstalled()) {}

    return isUsable;
  }
 private void isWechatInstalled(CallbackContext callbackContext) {
   if (!api.isWXAppInstalled()) {
     callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
   } else {
     callbackContext.success(SUCCESS);
   }
   currentCallbackContext = callbackContext;
 }
Exemplo n.º 4
0
 public static void WXImageShare(Context context, WXMediaMessage msg) {
   api = WXAPIFactory.createWXAPI(context, APP_WX_ID);
   api.registerApp(APP_WX_ID);
   if (api.isWXAppInstalled()) {
     SendMessageToWX.Req req = new SendMessageToWX.Req();
     req.transaction = buildTransaction("img");
     req.message = msg;
     req.scene = SendMessageToWX.Req.WXSceneSession;
     api.sendReq(req);
   } else {
     Toast.makeText(context, "您没有安装微信客户端,无法使用该功能!", Toast.LENGTH_LONG).show();
   }
 }
        @Override
        public void onClick(View v) {

          switch (v.getId()) {

              // 支付宝支付
            case R.id.zhufu_rel_01:
              boolean connectInternet =
                  NetworkStateUtil.isConnectInternet(LineTeachSelePlayActivity.this);
              if (!connectInternet) {
                WidgetUtil.ToastMessage(
                    LineTeachSelePlayActivity.this,
                    getResources().getString(R.string.connect_net_fail));
                return;
              } else {
                // 2代表支付宝支付
                PostOrderId(orderBean, PLAY_ZH);
                Payment(orderBean);
              }

              break;
              // 微信支付

            case R.id.zhufu_rel_02:
              boolean connectInternet1 =
                  NetworkStateUtil.isConnectInternet(LineTeachSelePlayActivity.this);
              if (!connectInternet1) {
                WidgetUtil.ToastMessage(
                    LineTeachSelePlayActivity.this,
                    getResources().getString(R.string.connect_net_fail));
                return;
              } else {

                if (!msgApi.isWXAppInstalled()) {

                  toastLong("您还没有安装微信!");
                  // 提醒用户没有按照微信
                  return;
                } else {
                  // 3代表微信支付
                  PostOrderId(orderBean, PLAY_WX);
                  WeiXinPlay(orderBean);
                }
              }

              break;
          }
        }
  private void auth(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 微信登录授权
    // params[0] -- scope
    // params[1] -- state
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // final IWXAPI api = ShareWechatPlugin.api;
    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    String scope = params.getString(0);
    String state = params.getString(1);

    final SendAuth.Req req = new SendAuth.Req();
    req.scope = scope;
    req.state = state;

    boolean success = api.sendReq(req);
    if (success) {
      // Log.i(TAG, "发送成功.");
      //			System.out.println("授权请求发送成功");
      sendNoResultPluginResult(callbackContext);
    } else {
      //			System.out.println("授权请求发送失败了");
      callbackContext.error(ERR_SENT_FAILED);
    }

    //		currentCallbackContext = callbackContext;
    //		try {
    //            boolean success = api.sendReq(req);
    //            if (!success) {
    //                //callbackContext.error(ERR_SENT_FAILED);
    //                //return;
    //            }else{
    //            	//callbackContext.success(SUCCESS);
    //            }
    //        } catch (Exception e) {
    //            //callbackContext.error(e.getMessage());
    //            return;
    //        }

  }
  private void pay(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 微信支付
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // 参数,传入时按顺序(partnerId,prepayId,timeStamp,nonceStr,sign)

    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    final PayReq req = new PayReq();
    req.appId = getAppId();
    req.partnerId = params.getString(0);
    req.prepayId = params.getString(1);
    req.timeStamp = params.getString(2);
    req.nonceStr = params.getString(3);
    req.sign = params.getString(4);
    req.packageValue = "Sign=WXPay";

    try {
      boolean success = api.sendReq(req);
      if (!success) {
        callbackContext.error(ERR_SENT_FAILED);
        return;
      } else {
        callbackContext.success(SUCCESS);
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    currentCallbackContext = callbackContext;
  }
Exemplo n.º 8
0
 /** 分享朋友圈 */
 private void shareToFriendQuan(
     Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
   Bitmap b = null;
   ByteArrayOutputStream output = null;
   try {
     IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
     api.registerApp(APP_ID);
     WXMediaMessage msg = null;
     if (!api.isWXAppInstalled()) {
       Toast.makeText(
               act,
               act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
               Toast.LENGTH_SHORT)
           .show();
       return;
     }
     switch (type) {
       case Share_Text_Type:
         WXTextObject textObj = new WXTextObject();
         textObj.text = title;
         msg = new WXMediaMessage();
         msg.mediaObject = textObj;
         msg.description = title;
         break;
       case Share_Webpage_Type:
         WXWebpageObject webpage = new WXWebpageObject();
         webpage.webpageUrl = shareUrl;
         msg = new WXMediaMessage(webpage);
         msg.title = title;
         msg.description = des;
         break;
     }
     if (type == Share_Webpage_Type && mDrawable != null) {
       BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
       b = bitmapDrawable.getBitmap();
       if (b == null) {
         bitmapDrawable =
             ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
         b = bitmapDrawable.getBitmap();
       }
       output = new ByteArrayOutputStream();
       b = zoomImg(b, 100);
       b.compress(CompressFormat.PNG, 70, output);
       byte[] result = output.toByteArray();
       msg.thumbData = result;
     }
     SendMessageToWX.Req req = new SendMessageToWX.Req();
     if (type == Share_Text_Type) req.transaction = buildTransaction("text");
     else if (type == Share_Webpage_Type) req.transaction = buildTransaction("webpage");
     req.message = msg;
     req.scene = SendMessageToWX.Req.WXSceneTimeline;
     api.sendReq(req);
   } catch (Exception e) {
     Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
   } finally {
     if (b != null) {
       b = null;
     }
     if (output != null) {
       try {
         output.close();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
Exemplo n.º 9
0
  private void shareToWeixin(
      Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
    Bitmap b = null;
    try {
      IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
      api.registerApp(APP_ID);
      if (!api.isWXAppInstalled()) {
        Toast.makeText(
                act,
                act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
                Toast.LENGTH_SHORT)
            .show();
        return;
      }
      WXMediaMessage msg = null;
      switch (type) {
        case Share_Text_Type:
          WXTextObject textObj = new WXTextObject();
          textObj.text = title;
          msg = new WXMediaMessage();
          msg.mediaObject = textObj;
          msg.description = title;
          break;
        case Share_Webpage_Type:
          WXWebpageObject webpage = new WXWebpageObject();
          webpage.webpageUrl = shareUrl;
          msg = new WXMediaMessage(webpage);
          msg.title = title;
          msg.description = des;
          break;
      }

      if (type == Share_Webpage_Type && mDrawable != null) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
        b = bitmapDrawable.getBitmap();
        b = zoomImg(b, 100);
        if (b == null) {
          bitmapDrawable =
              ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
          b = bitmapDrawable.getBitmap();
        }
        msg.thumbData = bmpToByteArray(b, true);
      }
      SendMessageToWX.Req req = new SendMessageToWX.Req();
      if (type == Share_Text_Type) req.transaction = buildTransaction("text");
      else if (type == Share_Webpage_Type) {
        req.transaction = buildTransaction("webpage");
        msg.title = title;
        msg.description = des;
      }
      req.message = msg;
      req.scene = SendMessageToWX.Req.WXSceneSession;
      api.sendReq(req);
    } catch (Exception e) {
      e.printStackTrace();
      Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
    } finally {
      if (b != null) {
        b = null;
      }
    }
  }
  private void share(JSONArray params, final CallbackContext callbackContext)
      throws JSONException, MalformedURLException, IOException {
    // 目前只支持 WXWebpageObject 分享
    // params[0] -- 分享的目标场景(0会话,1朋友圈,2微信收藏)
    // params[1] -- 分享内容的目标 url
    // params[2] -- 标题
    // params[3] -- 描述
    // params[4] -- 图片url
    // 返回值:0-成功;1-微信未安装;2-发送失败
    // final IWXAPI api = ShareWechatPlugin.api;
    if (!api.isWXAppInstalled()) {
      callbackContext.error(ERR_WECHAT_NOT_INSTALLED);
      return;
    }
    if (params == null) {
      callbackContext.error(ERR_INVALID_OPTIONS);
      return;
    }

    int targetScene = params.getInt(0);
    String webUrl = params.getString(1);
    String title = params.getString(2);
    String description = params.getString(3);
    String imgUrl = params.getString(4);

    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = webUrl;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = description;

    try {
      Bitmap bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
      Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
      bmp.recycle();
      msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    final SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = msg;
    switch (targetScene) {
      case 0:
        req.scene = SendMessageToWX.Req.WXSceneSession;
        break;
      case 1:
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        break;
      case 2:
        req.scene = SendMessageToWX.Req.WXSceneFavorite;
        break;
      default:
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
    }

    // run in background
    //        cordova.getThreadPool().execute(new Runnable() {
    //            @Override
    //            public void run() {
    //                if (api.sendReq(req)) {
    //                    Log.i(TAG, "wechat message sent successfully.");
    //                    //send success
    //                    callbackContext.success(NO_RESULT);
    //                } else {
    //                    Log.i(TAG, "wechat message sent failed.");
    //                    // send error
    //                    callbackContext.error(ERR_SENT_FAILED);
    //                }
    //            }
    //        });

    try {
      boolean success = api.sendReq(req);
      if (!success) {
        callbackContext.error(ERR_SENT_FAILED);
        return;
      } else {
        callbackContext.success(SUCCESS);
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return;
    }

    currentCallbackContext = callbackContext;
  }