android 分享功能

上一篇讲了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;
		}
	}

}

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

时间: 2024-10-09 08:16:47

android 分享功能的相关文章

Android -- 分享功能和打开指定程序

打开指定程序                                                                                Intent intent = new Intent(); ComponentName cmp = new ComponentName("com.sina.weibo","com.sina.weibo.EditActivity"); intent.setAction(Intent.ACTION_M

Android分享功能,微博、QQ、QQ空间等社交平台分享之入门与进阶

前言 如何能迅速做一个分享功能,那主要就是根据第一个步骤来做就好了,因为友盟的sdk包已经集成很多的内容了,只要你有相应的app和平台账号就可以能马上分享.进阶的内容就是从第三部分开始,这些内容不难,目的就是为了更好的优化分享体验. 1.先做一个简单的分享效果. 1.1 创建一个Android项目 以下项目所需要的jar包,图片等资源可到该网站下载SDK即可. http://dev.umeng.com/social/android/quick-integration?spm=0.0.0.0.Lk

Android 分享功能的实现

I : 利用隐式Intent实现分享    1.应用实现分享的功能        1 /* 实现分享功能 */ 2 3 Intent intent = new Intent(); 4 intent.setAction(Intent.ACTION_SEND); 5 6 // 分享文本 7 intent.setType("text/plain"); // text/html ... 8 intent.putExtra(Intent.EXTRA_SUBJECT, "我要分享&quo

Android分享功能实现

通过系统分享组件实现分享功能 Intent.createChooser() 方法用来弹出系统分享列表. createChooser(Intent target, CharSequence title, IntentSender sender) 参数. 常规方法 public void share(Context context){ Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendInt

Android分享功能的一点总结

前段时间给以前的App加了分享功能,与大家分享一些心得. 实现分享功能有三种方式: 1.调用Android自带的分享接口,这种方式最简单,它是直接调用App的发信息功能,把我们的链接通过信息方式发出去,因此它的分享效果比较差.同时它只能发送给手机上已安装的社交App.最坑的是有些App对此功能是屏蔽的,比如微信朋友圈.代码很简单,网上一搜就有,我就不赘述了. 2.通过第三方SDK来分享,国内比较常用的有友盟和MO.首先要去申请开发,然后下载SDK再调用相应接口就行,缺点是支持部分APP(国内的大

分享一套完整的android 分享功能代码

微信分享代码: <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.han

在Android中如何实现分享功能

Android应用中能很方便的完成这些功能,很多的应用中都有"分享"功能?如何分享呢?下面给大家说说看.最近有人问到Android分享功能用那个比较好,使用Android开发自带的Intent来进行分享还是借助第三方呢,直接上代码: 一.使用Intent直接和第三方应用进行通信: /** * 分享功能 * * @param context *            上下文 * @param activityTitle *            Activity的名字 * @param

Android APP代码拨打电话、打开手机分享功能等隐式意图

Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+110)); startActivity(intent); } Android APP打开电话薄: Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivity(intent); Android

在Android中使App高速、简单地支持新浪微博、微信、QQ、facebook等十几个主流社交平台的分享功能

前言 在如今的APP或者游戏中,分享功能差点儿已经成为标配.分享功能不但能够满足用户的需求.也能够为产品带来很多其它的用户,甚至能够对用户的行为.活跃度.年龄段等情况进行数据统计,使得软件公司能够对产品进行更精准的定位.今天我们就来简单剖析市场上一款优秀的分享SDK以及其集成过程.这款分享SDK就是友盟的社会化分享组件. 友盟社会化分享,帮助移动应用高速具备分享.登录.评论.喜欢等社交功能,提升用户粘度.助力产品推广,并提供实时.全面的社会化数据统计分析服务. 很多其它请訪问友盟社会化组件官网.