Android第三方应用分享图文到微信朋友圈 & 微信回调通知分享状态


第一步:去微信开放平台(https://open.weixin.qq.com/)创建应用,并拿到AppId。

第二步:导入微信支持jar文件

。。。这里假设读者已经了解一般的第三方API开发,一些步骤就省略了。

第三步:发送图文消息到微信朋友圈(按钮触发事件里加入以下代码,请确保应用签名与在开放平台注册的应用签名一致,且AppId也应该对应)

IWXAPI api = WXAPIFactory.createWXAPI(context, APP_ID, false);
    api.registerApp(APP_ID);
    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = "http://www.xxxx.com/wap/showShare/;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = "我要约";
    msg.description = "我要约分享";
    try
    {
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);
      Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
      bmp.recycle();
      msg.setThumbImage(thumbBmp);
    } 
    catch (Exception e)
    {
      e.printStackTrace();
    }
    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = String.valueOf(System.currentTimeMillis());
    req.message = msg;
    req.scene = SendMessageToWX.Req.WXSceneTimeline;
    api.sendReq(req);

如果一切顺利,就会看到了文章开头类似的图片。

这时点击取消或分享没有任何提示。

第四步:加提示回调页面。

(1)新建一个包

规则是你的程序包名加wxapi【例如:com.xxx.wxapi】

(2)在新建的包里面添加一个类,WXEntryActivity.java,名字必须一致,这是微信开放平台规定的。

package com.xxx.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.lbt.hairdesigner.R;
import com.lbt.hairdesigner.utils.MySetting;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler
{

  private IWXAPI api;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    api = WXAPIFactory.createWXAPI(this, APP_ID, false);
    api.registerApp(APP_ID);
    api.handleIntent(getIntent(), this);
  }

  @Override
  public void onReq(BaseReq req)
  {
  }

  @Override
  public void onResp(BaseResp resp)
  {
    int result = 0;

    switch (resp.errCode)
    {
    case BaseResp.ErrCode.ERR_OK:
      result = R.string.errcode_success;
      break;
    case BaseResp.ErrCode.ERR_USER_CANCEL:
      result = R.string.errcode_cancel;
      break;
    case BaseResp.ErrCode.ERR_AUTH_DENIED:
      result = R.string.errcode_deny;
      break;
    default:
      result = R.string.errcode_unknown;
      break;
    }

    Toast.makeText(this, result, Toast.LENGTH_LONG).show();
    finish();
    overridePendingTransition(R.anim.change_in, R.anim.change_out);
  }

}
(3)string.xml中添加一些字符item
<string name="errcode_success">发送成功</string>
  <string name="errcode_cancel">发送取消</string>
  <string name="errcode_deny">发送被拒绝</string>
  <string name="errcode_unknown">发送返回</string>

(4)附上change_in.xml 和change_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/decelerate_interpolator" >

  <scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%p"
    android:pivotY="50%p"
    android:toXScale="1.0"
    android:toYScale="1.0" />

</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/decelerate_interpolator"
  android:zAdjustment="top" >

  <scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%p"
    android:pivotY="50%p"
    android:toXScale="1.0"
    android:toYScale="1.0" />

  <alpha
    android:duration="@android:integer/config_shortAnimTime"
    android:fromAlpha="1.0"
    android:toAlpha=".0" />

</set>
最后:不管你在哪个地方调用第三步分享,微信都会有回调提示了。


===========THE===END=======================

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值