android 微信分享详情

  1. 首先要进行在微信平台上创建移动应用,点击一下连接看详细介绍。http://blog.csdn.net/huang3513/article/details/53064439

  2. 微信分享朋友圈和微信好友要进行SDK的下载,libammsdk.jar的jar包,集成到libs当中,集成之后就可以在项目中集成相应代码了。

  3. 分享微信朋友圈和微信好友
    (1)要在项目包下创建一个wxapi包名,里面创建一个WXEntryActivity类,并且要实现IWXAPIEventHandler这个接口。实现上面的接口,会复写onReq()和onResp()两个方法。其中onReq()方法,微信发送请求到第三方应用时,会回调到该方法。而onResp()第三方应用发送到微信的请求处理后的响应结果,会回调到该方法。

    示例代码:

@Override
	public void onResp(BaseResp resp) {
		int result = 0;
		switch (resp.errCode){
		case BaseResp.ErrCode.ERR_OK:
			result = "发送成功";
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = "发送取消";
			break;
			
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = "发送被拒绝";
			break;
		default:
			result = "发送返回";
			break;
		}
		Toast.makeText(this, result, Toast.LENGTH_LONG).show();
		finish();
	}

(2)IWXAPI是第三方app和微信通信的openapi接口对象

IWXAPI api = WXAPIFactory.createWXAPI(this,wxb91199337fe56a90, false);
//然后要进行注册(wxb91199337fe56a69==微信开发平台上的AppId);
api.registerApp(wxb91199337fe56a69);
//进行参数的判断
title=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TITLE);		description=getIntent().getStringExtra(HDCivilizationConstants.SHARE_DESRIPTION);		shareFilePath=getIntent().getStringExtra(HDCivilizationConstants.SHARE_IMG_PATH);		webpageUrl=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TARGET_URL);
sceneFlag=getIntent().getBooleanExtra(HDCivilizationConstants.SHARE_SCENEFLAG, false);
shareType=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TYPE);

(3)然后进行具体参数判断和使用

private void share2Wx(){
		 // 通过WXAPIFactory工厂,获取IWXAPI的实例
		 //首先进行检测微信的版本
		 int wxSdkVersion = api.getWXAppSupportAPI();
	     if (api.isWXAppSupportAPI() && api.isWXAppInstalled()){
				 //进行支持:看是否分享到朋友圈还是微信的好友
			    if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_WEBURL)){
					//网络类型
					WXWebpageObject webpage = new WXWebpageObject();
					webpage.webpageUrl =this.webpageUrl;
					final WXMediaMessage msg = new WXMediaMessage(webpage);
					msg.mediaObject=webpage;

					//待定
					msg.title = title;
					msg.description =description ;
					if(shareFilePath!=null && !shareFilePath.equals("")){
//						if(shareFilePath.startsWith("http://")){
//							new Thread(){
//								@Override
//								public void run() {
//										try {
//											Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());
//											Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//											bmp.recycle();
//											msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//										} catch (IOException e) {
//											e.printStackTrace();
//										}
//								}
//							}.start();
//						}else{
//							//本地路径
//							System.out.println("WX FILE PATH:"+shareFilePath);
//							Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
//							Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);
//							bmp.recycle();
//							msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//						}
						//本地路径
						System.out.println("WX FILE PATH:"+shareFilePath);
						Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
						Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);
						bmp.recycle();
						msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
					}else{
						Bitmap bmp =BitmapFactory.decodeResource(//
								UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);
						Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 69, true);
						bmp.recycle();
						msg.thumbData=Util.bmpToByteArray(thumbBmp, true);
					}
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("webpage");
					req.message = msg;
					req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
					api.sendReq(req);
					finish();
				}else if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_IMAGE)){

					//网络类型
					final WXImageObject imgObject = new WXImageObject();
					WXMediaMessage msg = new WXMediaMessage(imgObject);

					//
					if(shareFilePath!=null && !shareFilePath.equals("")){
						System.out.println("WX FILE PATH:"+shareFilePath);
//						if(shareFilePath.startsWith("http://")){
//							new Thread(){
//								@Override
//								public void run() {
//									try {
//										Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());
//										Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//										bmp.recycle();
//										imgObject.imagePath=shareFilePath;
//										imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
//										imgObject.imageUrl=shareFilePath;
//									} catch (IOException e) {
//										e.printStackTrace();
//									}
//								}
//							}.start();

//						}else{
//							//本地路径
//							Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
//							Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//							bmp.recycle();
//							imgObject.imagePath=shareFilePath;
//							imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
//							imgObject.imageUrl=shareFilePath;
//						}

						Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
						Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
						bmp.recycle();
						imgObject.imagePath=shareFilePath;
						imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
						imgObject.imageUrl=shareFilePath;
					}else{
						Bitmap bmp =BitmapFactory.decodeResource(//
								UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);
						Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);
						bmp.recycle();
						imgObject.imagePath=shareFilePath;
						imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
						imgObject.imageUrl=shareFilePath;
					}

//					if(!FileUtils.getInstance().isExistsFile(MyApplication.shareZinCodePath)){
//						try {
//							FileUtils.getInstance().writeToDir(getAssets().open("mine_share_code.png"),MyApplication.shareZinCodePath);
//						} catch (IOException e){
//							e.printStackTrace();
//						}
//					}
//					Bitmap bmp =BitmapFactory.decodeFile(MyApplication.shareZinCodePath);
//					Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);
//					imgObject.imagePath=MyApplication.shareZinCodePath;
//					imgObject.imageData=Util.bmpToByteArray(thumbBmp, true);
//					imgObject.imageUrl=MyApplication.shareZinCodePath;
//					bmp.recycle();

					msg.thumbData=imgObject.imageData;
					//待定
					msg.title = "测试app";
					msg.description ="测试app 某某模块" ;
					//待定
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("webpage");
					req.message = msg;
					req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
					api.sendReq(req);
					finish();
				}

	     }else{
	    	 	Toast.makeText(UiUtils.getInstance().getContext(),
							"您的微信的版本过低版本,或者未装微信客户端", Toast.LENGTH_LONG).show();
	     }
	}

以上是基本的使用步骤,可能写的有点乱,下面给大家贴出完成的代码:

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	
	private Button gotoBtn, regBtn, launchBtn, checkBtn;
	
	// IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
	private boolean flag;
	private String title;
	private String description;
	private String shareFilePath;
	private String webpageUrl;
	private boolean sceneFlag;  //判断朋友圈的状态 true时是朋友圈 false微信好友
    private String shareType="";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //通过WXAPIFactory工厂,获取IWXAPI的实例
    	api = WXAPIFactory.createWXAPI(this, HDCivilizationConstants.WXAPP_ID, false);
    	//进行注册
    	api.registerApp(HDCivilizationConstants.WXAPP_ID);
        api.handleIntent(getIntent(), this);
        
		//进行参数的判断
		title=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TITLE);
		description=getIntent().getStringExtra(HDCivilizationConstants.SHARE_DESRIPTION);
		shareFilePath=getIntent().getStringExtra(HDCivilizationConstants.SHARE_IMG_PATH);
		webpageUrl=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TARGET_URL);
		sceneFlag=getIntent().getBooleanExtra(HDCivilizationConstants.SHARE_SCENEFLAG, false);
		shareType=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TYPE);
		share2Wx();
    }

	
	@Override
	protected void onNewIntent(Intent intent){
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	// 微信发送请求到第三方应用时,会回调到该方法
	@Override
	public void onReq(BaseReq req) {
//		Toast.makeText(UiUtils.getContext(), "发送了请求", Toast.LENGTH_SHORT).show();
	}

	//第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		int result = 0;
		switch (resp.errCode){
		case BaseResp.ErrCode.ERR_OK:
			result = "发送成功";
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = "发送取消";
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = "发送被拒绝";
			break;
		default:
			result = "发送返回";
			break;
		}
		Toast.makeText(this, result, Toast.LENGTH_LONG).show();
		finish();
	}
	
	
	//新添加的方法
	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
	
	/**
	 *
	 * @param
	 */
	private void share2Wx(){
		 // 通过WXAPIFactory工厂,获取IWXAPI的实例
		 //首先进行检测微信的版本
		 int wxSdkVersion = api.getWXAppSupportAPI();
	     if (api.isWXAppSupportAPI() && api.isWXAppInstalled()){
				 //进行支持:看是否分享到朋友圈还是微信的好友
			    if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_WEBURL)){
					//网络类型
					WXWebpageObject webpage = new WXWebpageObject();
					webpage.webpageUrl =this.webpageUrl;
					final WXMediaMessage msg = new WXMediaMessage(webpage);
					msg.mediaObject=webpage;

					//待定
					msg.title = title;
					msg.description =description ;
					if(shareFilePath!=null && !shareFilePath.equals("")){
//						if(shareFilePath.startsWith("http://")){
//							new Thread(){
//								@Override
//								public void run() {
//										try {
//											Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());
//											Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//											bmp.recycle();
//											msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//										} catch (IOException e) {
//											e.printStackTrace();
//										}
//								}
//							}.start();
//						}else{
//							//本地路径
//							System.out.println("WX FILE PATH:"+shareFilePath);
//							Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
//							Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);
//							bmp.recycle();
//							msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//						}
						//本地路径
						System.out.println("WX FILE PATH:"+shareFilePath);
						Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
						Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);
						bmp.recycle();
						msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
					}else{
						Bitmap bmp =BitmapFactory.decodeResource(//
								UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);
						Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 69, true);
						bmp.recycle();
						msg.thumbData=Util.bmpToByteArray(thumbBmp, true);
					}
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("webpage");
					req.message = msg;
					req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
					api.sendReq(req);
					finish();
				}else if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_IMAGE)){

					//网络类型
					final WXImageObject imgObject = new WXImageObject();
					WXMediaMessage msg = new WXMediaMessage(imgObject);

					//
					if(shareFilePath!=null && !shareFilePath.equals("")){
						System.out.println("WX FILE PATH:"+shareFilePath);
//						if(shareFilePath.startsWith("http://")){
//							new Thread(){
//								@Override
//								public void run() {
//									try {
//										Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());
//										Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//										bmp.recycle();
//										imgObject.imagePath=shareFilePath;
//										imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
//										imgObject.imageUrl=shareFilePath;
//									} catch (IOException e) {
//										e.printStackTrace();
//									}
//								}
//							}.start();

//						}else{
//							//本地路径
//							Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
//							Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
//							bmp.recycle();
//							imgObject.imagePath=shareFilePath;
//							imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
//							imgObject.imageUrl=shareFilePath;
//						}

						Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);
						Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);
						bmp.recycle();
						imgObject.imagePath=shareFilePath;
						imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
						imgObject.imageUrl=shareFilePath;
					}else{
						Bitmap bmp =BitmapFactory.decodeResource(//
								UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);
						Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);
						bmp.recycle();
						imgObject.imagePath=shareFilePath;
						imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);
						imgObject.imageUrl=shareFilePath;
					}

//					if(!FileUtils.getInstance().isExistsFile(MyApplication.shareZinCodePath)){
//						try {
//							FileUtils.getInstance().writeToDir(getAssets().open("mine_share_code.png"),MyApplication.shareZinCodePath);
//						} catch (IOException e){
//							e.printStackTrace();
//						}
//					}
//					Bitmap bmp =BitmapFactory.decodeFile(MyApplication.shareZinCodePath);
//					Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);
//					imgObject.imagePath=MyApplication.shareZinCodePath;
//					imgObject.imageData=Util.bmpToByteArray(thumbBmp, true);
//					imgObject.imageUrl=MyApplication.shareZinCodePath;
//					bmp.recycle();

					msg.thumbData=imgObject.imageData;
					//待定
					msg.title = "测试app";
					msg.description ="测试app 某某模块" ;
					//待定
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("webpage");
					req.message = msg;
					req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
					api.sendReq(req);
					finish();
				}

	     }else{
	    	 	Toast.makeText(UiUtils.getInstance().getContext(),
							"您的微信的版本过低版本,或者未装微信客户端", Toast.LENGTH_LONG).show();
	     }
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值