分享一套完整的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.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); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; // msg.title = "Will be ignored"; msg.description =context.getString(R.string.app_share_content); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("text"); req.message = msg; req.scene =flag; api.sendReq(req); } 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); } }; }
来源:成都app开发 www.cxb360.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。