码迷,mamicode.com
首页 > 移动开发 > 详细

android 分享功能

时间:2015-05-20 22:32:33      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:android   微信   qq   新浪微博   分享   

       上一篇讲了android第三方登录,这一篇大概说一下分享。需要注意几点: 

1.登录和分享功能都必须是签名之后的apk,才能用 

2.再吐槽一下,微信SDK做的真他妈的变态,和登录一样,必须是例如我的apk,包名(也就是AndroidManifest.xml中的包名)是com.kingdowin.gosu,你项目中必须有一个包名是com.kingdowin.gosu.wxapi,下面有一个类是WXEntryActivity.java,里面写登录或分享以及获得返回响应结果(public void onResp(BaseResp resp) {})微信代码,我不得不把自己的MainActivity改为WXEntryActivity。否则就获取不到用户信息,记好是必须

3.微信分享是文本能分享而图片不能,那是因为你分享的图片太大了,SDK协议中对缩略图的大小作了限制,大小不能超过32K。另外限制的还有title、description等参数的长度。另外Bitmap的compress方法不支持PNG,只支持JPG。

4.qq分享如果包含图片似乎只支持手机里的本地图片而不支持二进制

效果图:

技术分享

微信分享代码:

<pre name="code" class="html">public class WXEntryActivity extends Activity implements OnClickListener,
		IWXAPIEventHandler {

private IWXAPI api;
api = WXAPIFactory.createWXAPI(this, Constant.WEIXIN_APP_ID, false);
		api.handleIntent(getIntent(), this);
protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		switch (req.getType()) {

		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			break;
		default:
			break;
		}
	}

	@Override
	public void onResp(BaseResp resp) {
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			if (MyApplication.loginShare.equals("login")) {
			String code = ((SendAuth.Resp) resp).code;
			new WeiXinLogin(this).getWeiXinInfo(code);
			Toast.makeText(this, R.string.auth_success, Toast.LENGTH_SHORT)
					.show();
			}else{
				Toast.makeText(this, R.string.share_success, Toast.LENGTH_SHORT)
				.show();
			}
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			if (MyApplication.loginShare.equals("login")) {
			Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT)
					.show();
			}else{
				Toast.makeText(this, R.string.share_cancel, Toast.LENGTH_SHORT)
				.show();
			}
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			if (MyApplication.loginShare.equals("login")) {
			Toast.makeText(this, R.string.auth_failure, Toast.LENGTH_SHORT)
					.show();
			}else{
				Toast.makeText(this, R.string.share_failure, Toast.LENGTH_SHORT)
				.show();
			}
			break;
		default:
			break;
		}
	}
}


public class WeiXinShare{
	
	private Activity context;
	private IWXAPI api;
	private static final int THUMB_SIZE = 150;
	
	public WeiXinShare(Activity context){
		this.context=context;
		api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
		api.registerApp(Constant.WEIXIN_APP_ID);
	}
	public void shareText(int flag){
	WXTextObject textObj = new WXTextObject();
	textObj.text = context.getString(R.string.app_share_content);

	// ??WXTextObject???????????WXMediaMessage????
	WXMediaMessage msg = new WXMediaMessage();
	msg.mediaObject = textObj;
	// ??????????????????title??β???????
	// msg.title = "Will be ignored";
	msg.description =context.getString(R.string.app_share_content);

	// ???????Req
	SendMessageToWX.Req req = new SendMessageToWX.Req();
	req.transaction = buildTransaction("text"); // transaction???????Ψ???????????
	req.message = msg;
	req.scene =flag;
	api.sendReq(req);
	}
	// ????api??????????????
	
	public void shareImg(int flag){
		Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon);
		WXImageObject imgObj = new WXImageObject(bmp);
		
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		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 =flag;
		api.sendReq(req);
//		context.finish();
	}
	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
	public void shareWebToFriend(String shareContext,String url){
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = url;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title =context.getString(R.string.app_gosu);
		msg.description =shareContext;
		Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon);
		msg.thumbData = Util.bmpToByteArray(thumb, true);
		
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = 0;
		api.sendReq(req);
	}
	public void shareWebToZone(String shareContext,String url){
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl =url ;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title =shareContext;
		Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon);
		msg.thumbData = Util.bmpToByteArray(thumb, true);
		
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = 1;
		api.sendReq(req);
}
}



qq分享代码:

public class QShare {

	private Context context;
	public static Tencent mTencent;
	private String fileUrl;
	private int mExtarFlag = 0x00;

	public QShare(Context context) {
		this.context = context;
		if (mTencent == null) {
			mTencent = Tencent.createInstance(Constant.QQ_APP_ID, context);
		}
		fileUrl=MyApplication.photo_path+"app_icon.png";
	}

	public void shareToQ(String shareContent,String url) {
		final Bundle params = new Bundle();
		params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
		params.putString(QQShare.SHARE_TO_QQ_TITLE, context.getString(R.string.app_gosu));
		params.putString(QQShare.SHARE_TO_QQ_SUMMARY,shareContent);
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, fileUrl);
		params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,url);
		mTencent.shareToQQ((Activity) context, params, qqShareListener);
	}
	public void shareToQZone(String shareContent,String url) {
		final Bundle params = new Bundle();
		params.putString(QQShare.SHARE_TO_QQ_TITLE, context.getString(R.string.app_gosu));
		params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareContent);
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, fileUrl);
		params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,url);
		params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, mExtarFlag|= QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
		mTencent.shareToQQ((Activity) context, params, qqShareListener);
	}

	IUiListener qqShareListener = new IUiListener() {
		@Override
		public void onCancel() {
			Toast.makeText(context,
					context.getString(R.string.share_cancel),Toast.LENGTH_LONG);
		}

		@Override
		public void onComplete(Object response) {
			// TODO Auto-generated method stub
			Toast.makeText(context,
					context.getString(R.string.share_success),Toast.LENGTH_LONG);
		}

		@Override
		public void onError(UiError e) {
			// TODO Auto-generated method stub
			Toast.makeText(context,
					context.getString(R.string.share_failure),Toast.LENGTH_LONG);
		}
	};
}

微博分享代码:
public class WeiBoShare implements IWeiboHandler.Response {

	private IWeiboShareAPI mWeiboShareAPI;
	private Context context;

	public WeiBoShare(Context context) {
		this.context = context;
		mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(context,
				Constant.WEIBO_APP_KEY);
		mWeiboShareAPI.registerApp();
	}

	public void share(String shareContent) {
		WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
		TextObject textObject = new TextObject();
		textObject.text = shareContent;
		weiboMessage.textObject = textObject;
		weiboMessage.imageObject = getImageObj();
		// weiboMessage.mediaObject = getWebpageObj();
		SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
		// 鐢╰ransaction鍞竴鏍囪瘑涓?涓姹?
		request.transaction = String.valueOf(System.currentTimeMillis());
		request.multiMessage = weiboMessage;

		AuthInfo authInfo = new AuthInfo(context, Constant.WEIBO_APP_KEY,
				Constant.REDIRECT_URL, Constant.SCOPE);
		Oauth2AccessToken accessToken = AccessTokenKeeper
				.readAccessToken(context);
		Log.d("gaolei", "authInfo--------------" + authInfo);
		Log.d("gaolei", "accessToken--------------" + accessToken);
		String token = "";
		if (accessToken != null) {
			token = accessToken.getToken();
		}
		mWeiboShareAPI.sendRequest((Activity) context, request, authInfo,
				token, new WeiboAuthListener() {

					@Override
					public void onWeiboException(WeiboException e) {
						Log.d("gaolei",
								"onWeiboException--------------"
										+ e.getMessage());
					}

					@Override
					public void onComplete(Bundle bundle) {
						// TODO Auto-generated method stub
						Oauth2AccessToken newToken = Oauth2AccessToken
								.parseAccessToken(bundle);
						AccessTokenKeeper.writeAccessToken(context, newToken);
						Toast.makeText(
								context,
								"onAuthorizeComplete token = "
										+ newToken.getToken(), 0).show();
					}

					@Override
					public void onCancel() {
					}
				});

	}

//	private TextObject getTextObj() {
//		TextObject textObject = new TextObject();
//		textObject.text = context.getString(R.string.app_share_content);
//		return textObject;
//	}

	private ImageObject getImageObj() {
		ImageObject imageObject = new ImageObject();
		BitmapDrawable bitmapDrawable = (BitmapDrawable) context.getResources()
				.getDrawable(R.drawable.app_icon);
		imageObject.setImageObject(bitmapDrawable.getBitmap());
		return imageObject;
	}

	private WebpageObject getWebpageObj() {
		WebpageObject mediaObject = new WebpageObject();
		mediaObject.identify = Utility.generateGUID();
		mediaObject.title = context.getString(R.string.share_gosu);
		mediaObject.description = context.getString(R.string.app_share_content);
		// 璁剧疆 Bitmap 绫诲瀷鐨勫浘鐗囧埌瑙嗛瀵硅薄閲?
		mediaObject.setThumbImage(BitmapFactory.decodeResource(
				context.getResources(), R.drawable.app_icon));
		mediaObject.actionUrl = "http://www.kingdowin.com/";
		mediaObject.defaultText = context.getString(R.string.share_gosu);
		return mediaObject;
	}

	@Override
	public void onResponse(BaseResponse baseResp) {
		// TODO Auto-generated method stub
		switch (baseResp.errCode) {
		case WBConstants.ErrorCode.ERR_OK:
			Toast.makeText(context,
					context.getString(R.string.share_success),Toast.LENGTH_LONG);
			break;
		case WBConstants.ErrorCode.ERR_CANCEL:
			Toast.makeText(context,
					context.getString(R.string.share_cancel),Toast.LENGTH_LONG);
			break;
		case WBConstants.ErrorCode.ERR_FAIL:
			Toast.makeText(context,
					context.getString(R.string.share_failure),Toast.LENGTH_LONG);
			break;
		}
	}

}
                                           

                                 项目源码现在地址,欢迎光临................................
                                                      

android 分享功能

标签:android   微信   qq   新浪微博   分享   

原文地址:http://blog.csdn.net/gaolei1201/article/details/45873559

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