android 应用实现微信好友或朋友圈分享

官方的文档连接:https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&lang=zh_CN 

Java代码
private IWXAPI api; 
Java代码
// 微信注册初始化  
api = WXAPIFactory.createWXAPI(this, "wx5ce2ffa100e3f587", true);  
api.registerApp("wx5ce2ffa100e3f587");  

分享到微信里边的内容,其中flag 0是朋友圈,1是好友, 
分享前判断下是否有安装微信,没有就不提示用户 

Java代码
private void share2weixin(int flag) {  
    // Bitmap bmp = BitmapFactory.decodeResource(getResources(),  
    // R.drawable.weixin_share);  
  
    if (!api.isWXAppInstalled()) {  
        Toast.makeText(WXEntryActivity.this, "您还未安装微信客户端",  
                Toast.LENGTH_SHORT).show();  
        return;  
    }  
  
    WXWebpageObject webpage = new WXWebpageObject();  
    webpage.webpageUrl = "http://baidu.com";  
    WXMediaMessage msg = new WXMediaMessage(webpage);  
  
    msg.title = "title";  
    msg.description = getResources().getString(  
            R.string.app_share_weixin_txt);  
    Bitmap thumb = BitmapFactory.decodeResource(getResources(),  
            R.drawable.weixin_share);  
    msg.setThumbImage(thumb);  
    SendMessageToWX.Req req = new SendMessageToWX.Req();  
    req.transaction = String.valueOf(System.currentTimeMillis());  
    req.message = msg;  
    req.scene = flag;  
    api.sendReq(req);  
}

 


分享是否成功查询,可以通过IWXAPIEventHandler回调接口来处理 

Java代码 
@Override  
public void onReq(BaseReq arg0) {  
    // TODO Auto-generated method stub  
  
}  
  
@Override  
public void onResp(BaseResp resp) {  
  
    Log.v("caodongquan", "hello world");  
    String result = "";  
  
    switch (resp.errCode) {  
    case BaseResp.ErrCode.ERR_OK:  
        result = "errcode_success";  
        break;  
    case BaseResp.ErrCode.ERR_USER_CANCEL:  
        result = "errcode_cancel";  
        break;  
    case BaseResp.ErrCode.ERR_AUTH_DENIED:  
        result = "errcode_deny";  
        break;  
    default:  
        result = "errcode_unknown";  
        break;  
    }  
  
    // Toast.makeText(this, result, Toast.LENGTH_LONG).show();  
  
} 

注册回调监听事件 

Java代码
api.handleIntent(intent, this);  

注意事项: 
1.必须要在你注册的包名下,再新建一个"包名.wxapi"包,新建一个WXEntryActivity类 
2.keystore要配置好,具体是在"window--preferences--android--build--Custom debug keystore"选中keystore文件,然后使用签名文件打包,就行啦! 

你可能感兴趣的:(android)