码迷,mamicode.com
首页 > 微信 > 详细

Android 分享微信好友 朋友圈

时间:2015-03-12 19:01:10      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:

第三方应用,可以调用微信分享,把链接,文字,各种media,分享到微信好友或者微信朋友圈,步骤:

 

package com.edaixi.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;
import com.edaixi.activity.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WX_Share implements IWXAPIEventHandler{
    private IWXAPI api;
    private Context context;
    private int flag;//0 表示分享给微信好友哦,1表示分享到朋友圈

    public WX_Share(Context context,int flag) {
        super();
        this.context = context;
        this.flag = flag;
        registWX();
    }

    public void registWX() {
        api = WXAPIFactory.createWXAPI(context, Constants.APP_ID, true);
        api.registerApp(Constants.APP_ID);
    }

    public void share2WX() {  
        if (!api.isWXAppInstalled()) {  
            Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();  
            return;  
        }  
        WXWebpageObject webpage = new WXWebpageObject();  
        webpage.webpageUrl = "http://www.edaixi.com";  
        WXMediaMessage msg = new WXMediaMessage(webpage);  
      
        msg.title = "my test title...";  
        msg.description = context.getResources().getString(R.string.wx_share_content);  
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);  
        msg.setThumbImage(thumb);  
        
        SendMessageToWX.Req req = new SendMessageToWX.Req();  
        req.transaction = String.valueOf(System.currentTimeMillis());  
        req.message = msg;  
        req.scene = flag;  
        api.sendReq(req);  
    }

    @Override
    public void onReq(BaseReq arg0) {
        
    }

    @Override
    public void onResp(BaseResp resp) {
        Toast.makeText(context, "openid = " + resp.openId, Toast.LENGTH_SHORT).show();
        
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            Toast.makeText(context, "code = " + ((SendAuth.Resp) resp).code, Toast.LENGTH_SHORT).show();
        }
        
        String result = "";
        
        switch (resp.errCode) {
        case BaseResp.ErrCode.ERR_OK:
            result = "-sucessful-";
            break;
        case BaseResp.ErrCode.ERR_USER_CANCEL:
            result = "-cancle-";
            break;
        case BaseResp.ErrCode.ERR_AUTH_DENIED:
            result = "-error code-";
            break;
        default:
            result = "-unknow  error-";
            break;
        }
        
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    
    }  
}

 

Android 分享微信好友 朋友圈

标签:

原文地址:http://www.cnblogs.com/spring87/p/4332828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!