The previous article about Android third-party login, this article about share. Several points to note:
1. The login and share function must be the APK after the signature to use
2. Again, the SDK does a really fucking pervert, and the login must be For example my apk, The package name (that is, the package name in Androidmanifest.xml) is Com.kingdowin.gosu, you must have a package name in the project COM.KINGDOWIN.GOSU.WXAPI, the following is a class is Wxentryactivity.java, inside write login or Share and get the return response result (public void Onresp (Baseresp resp) {}) code, I had to change my mainactivity to wxentryactivity. Otherwise, the user information is not available, remember that you must
3. Sharing is the text can be shared and the picture cannot, that is because you share the picture is too big, theSDK agreement on the size of the thumbnail is limited, size can not exceed 32K. There are also restrictions on the length of the title, description and other parameters. In addition bitmap's Compress method does not support PNG, only JPG is supported.
4.QQ sharing If the containing picture appears to support only local images in the phone and does not support binary
:
Share code:
<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);p rotected void Onnewintent (Intent Intent) {super.onnewintent (Intent); Setintent ( Intent); Api.handleintent (Intent, this);} @Overridepublic void Onreq (basereq req) {switch (Req.gettype ()) {case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:break; Case Constantsapi.command_showmessage_from_wx:break;default:break;}} @Overridepublic 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 Weix Inshare (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;//?????????????????? The title?? Beta??????? Msg. title = "'ll be ignored"; Msg.description =context.getstring (r.string.app_share_content);//??????? Reqsendmessagetowx.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 Share code:
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 (Con Stant. qq_app_id, context);} fileurl=myapplication.photo_path+ "App_icon.png";} public void Sharetoq (String sharecontent,string URL) {Final bundle params = new Bundle ();p Arams.putint (qqshare.share_to_ Qq_key_type,qqshare.share_to_qq_type_default);p arams.putstring (Qqshare.share_to_qq_title, context.getString ( R.string.app_gosu));p arams.putstring (qqshare.share_to_qq_summary,sharecontent);p arams.putstring (QQShare.SHARE_ To_qq_image_local_url, FILEURL);p arams.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 ();p arams.putstring ( Qqshare.share_to_qq_title, Context.getstring (R.string.app_gosu));p arams.putstring (Qqshare.share_to_qq_summary, Sharecontent);p arams.putstring (Qqshare.share_to_qq_image_local_url, FILEURL); Params.putstring (Qqshare.share_to_qq_target_url,url);p arams.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 () {@Overridepublic void OnCancel () {Toast.maketext (context, Context.getstring (R.string.share_cancel), Toast.length_long);} @Overridepublic void OnComplete (Object response) {//TODO auto-generated Method Stubtoast.maketext (context, Context.getstring (r.string.share_success), Toast.length_long);} @Overridepublic void OnError (Uierror e) {//TODO auto-generated method Stubtoast.maketext (Context,context.getstring ( r.string.share_failure), Toast.length_long);}};}
Weibo share code:
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 Storyboard dress æ chimneys 瘑 Juan Juan Cha? Request.transaction = string.valueof (System.currenttimemillis ()); request.multimessage = WeiboMessage; AuthInfo AuthInfo = new AuthInfo (context, Constant.weibo_app_key,constant.redirect_url, constant.scope); O Auth2accesstoken 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 () {@Overridepublic void Onweiboexception (weiboexception e) {log.d ("Gaolei", "onweiboexception--------------" + e.getmessage ());} @Overridepublic void OnComplete (Bundle bundle) {//TODO auto-generated method Stuboauth2accesstoken Newtoken = Oauth2acce Sstoken.parseaccesstoken (bundle); Accesstokenkeeper.writeaccesstoken (context, newtoken); Toast.maketext (Context, "onauthorizecomplete token =" + Newtoken.gettoken (), 0). Show (); @Overridepublic 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)///OptIn play Xinjiang Bitmap Aya type Fermium 勫 trajectory draft embarrassing Lang Birkenau 嗛 hides silicon thin stated expiry refer? 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;} @Overridepublic void Onresponse (Baseresponse baseresp) {//TODO auto-generated method Stubswitch (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;}}}
Project source code is now the address, welcome to ..... ..... .............
Android sharing feature