基本信息
源码名称:android 微信分享示例代码
源码大小:0.43M
文件格式:.zip
开发语言:Java
更新时间:2015-04-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

不错的demo,大家可以借鉴一下


import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import clochase.wx.test.Constants;
import clochase.wx.test.R;
import clochase.wx.test.Util;

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.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
    /** Called when the activity is first created. */
	private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath();
	private static final int THUMB_SIZE = 150;
	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	private IWXAPI api;
	private Button gotoBtn;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 通过WXAPIFactory工厂,获取IWXAPI的实例
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
    	api.registerApp(Constants.APP_ID);
    	
    	gotoBtn = (Button) findViewById(R.id.goto_send_btn);
        gotoBtn.setOnClickListener(new View.OnClickListener() {
			
			
			public void onClick(View v) {
				
				String url = "http://img1.motou.com/haiwai/20110119/20110119210529576.jpg";
				
				try {
					WXImageObject imgObj = new WXImageObject();
					imgObj.imageUrl = url;
					
					WXMediaMessage msg = new WXMediaMessage();
					msg.mediaObject = imgObj;

					Bitmap bmp = BitmapFactory.decodeStream(new URL(url).openStream());
					Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
					bmp.recycle();
					msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
					
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("img");
					req.message = msg;
					req.scene =  SendMessageToWX.Req.WXSceneTimeline;
					api.sendReq(req);
					
					//finish();
				} catch (Exception e) {
					e.printStackTrace();
				}
		       // startActivity(new Intent(WXEntryActivity.this, SendToWXActivity.class));
		       // finish();
			}
		});
    	
    	api.handleIntent(getIntent(), this);
    }
    
   
  

    private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type   System.currentTimeMillis();
	}

 // 微信发送请求到第三方应用时,会回调到该方法
	
 	public void onReq(BaseReq req) {
 	}

 	// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
 	
 	public void onResp(BaseResp resp) {
 		System.out.println("&&&&&&&&&&&&&");
 		System.out.println("resp=" resp.errCode);
 		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();
 	}
}