Share a complete set of Android share function codes

Source: Internet
Author: User

Share code:

<pre name="Code" class="HTML"> Public classWxentryactivity extends Activity implements Onclicklistener, Iwxapieventhandler {PrivateIwxapi Api;api= Wxapifactory.createwxapi ( This, constant.weixin_app_id,false); Api.handleintent (Getintent (), This);protected voidonnewintent (Intent Intent) {super.onnewintent (Intent);        Setintent (Intent); Api.handleintent (Intent, This); } @Override Public voidOnreq (basereq req) {Switch(Req.gettype ()) { Caseconstantsapi.command_getmessage_from_wx: Break;  Caseconstantsapi.command_showmessage_from_wx: Break; default:             Break; }} @Override Public voidOnresp (Baseresp resp) {Switch(resp.errcode) { CaseBaseResp.ErrCode.ERR_OK:if(MyApplication.loginShare.equals ("Login") ) {String code=((SENDAUTH.RESP) Resp). Code; NewWeixinlogin ( This). Getweixininfo (code); Toast.maketext ( This-3.string. Auth_success, Toast.length_short). Show (); }Else{Toast.maketext ( This-3.string. Share_success, Toast.length_short). Show (); }             Break;  CaseBaseResp.ErrCode.ERR_USER_CANCEL:if(MyApplication.loginShare.equals ("Login") {Toast.maketext ( This-3.string. Auth_cancel, Toast.length_short). Show (); }Else{Toast.maketext ( This-3.string. Share_cancel, Toast.length_short). Show (); }             Break;  CaseBaseResp.ErrCode.ERR_AUTH_DENIED:if(MyApplication.loginShare.equals ("Login") {Toast.maketext ( ThisR.string. Auth_failure, Toast.length_short). Show (); }Else{Toast.maketext ( ThisR.string. Share_failure, Toast.length_short). Show (); }             Break; default:             Break; }    }} Public classweixinshare{PrivateActivity context; PrivateIWXAPI API; Private StaticFinalintThumb_size = Max;  PublicWeixinshare (Activity context) { This. context=context; API=Wxapifactory.createwxapi (context, constant.weixin_app_id);    Api.registerapp (constant.weixin_app_id); }     Public voidSharetext (intflag) {Wxtextobject Textobj=NewWxtextobject (); Textobj.text= Context.getstring (R.string. app_share_content); Wxmediamessage msg=NewWxmediamessage (); Msg.mediaobject=Textobj; //Msg.title = "'ll be ignored";Msg.description =context.getstring (R.string. app_share_content); Sendmessagetowx.req Req=NewSendmessagetowx.req (); Req.transaction= Buildtransaction ("text"); Req.message=msg; Req.scene=Flag;    Api.sendreq (req); }         Public voidShareimg (intflag) {Bitmap BMP=Bitmapfactory.decoderesource (Context.getresources (), R.drawable.app_icon); Wximageobject imgobj=NewWximageobject (BMP); Wxmediamessage msg=NewWxmediamessage (); Msg.mediaobject=imgobj; Bitmap thumbbmp= Bitmap.createscaledbitmap (BMP, Thumb_size, Thumb_size,true);        Bmp.recycle (); Msg.thumbdata= Util.bmptobytearray (Thumbbmp,true); Sendmessagetowx.req Req =NewSendmessagetowx.req (); Req.transaction=buildtransaction ("img"); Req.message=msg; Req.scene=Flag; Api.sendreq (req);//context.finish ();    }    Privatestring buildtransaction (final String type) {return(Type = =NULL) ? String.valueof (System.currenttimemillis ()): Type +System.currenttimemillis (); }     Public voidsharewebtofriend (String sharecontext,string url) {wxwebpageobject webpage=NewWxwebpageobject (); Webpage.webpageurl=URL; Wxmediamessage msg=Newwxmediamessage (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=NewSendmessagetowx.req (); Req.transaction= Buildtransaction ("webpage"); Req.message=msg; Req.scene=0;    Api.sendreq (req); }     Public voidsharewebtozone (String sharecontext,string url) {wxwebpageobject webpage=NewWxwebpageobject (); Webpage.webpageurl=URL; Wxmediamessage msg=Newwxmediamessage (webpage); Msg.title=Sharecontext; Bitmap Thumb=Bitmapfactory.decoderesource (Context.getresources (), R.drawable.app_icon); Msg.thumbdata= Util.bmptobytearray (Thumb,true); Sendmessagetowx.req Req=NewSendmessagetowx.req (); Req.transaction= Buildtransaction ("webpage"); Req.message=msg; Req.scene=1; Api.sendreq (req);}}

QQ Share code:

 Public classQshare {Privatecontext Context;  Public StaticTencent Mtencent; PrivateString FileUrl; Private intMextarflag =0x00;  PublicQshare (Context context) { This. Context =context; if(Mtencent = =NULL) {mtencent=tencent.createinstance (constant.qq_app_id, context); } FILEURL=myapplication.photo_path+"App_icon.png"; }     Public voidsharetoq (String sharecontent,string URL) {Final Bundleparams=NewBundle (); 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 voidsharetoqzone (String sharecontent,string URL) {Final Bundleparams=NewBundle (); 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=NewIuilistener () {@Override Public voidOnCancel () {Toast.maketext (context, context.getstring (R.string. Share_cancel), Toast.length_long); } @Override Public voidOnComplete (Object response) {//TODO auto-generated Method StubToast.maketext (Context, context.getstring (R.string. share_success), Toast.length_long); } @Override Public voidOnError (Uierror e) {//TODO auto-generated Method StubToast.maketext (Context, context.getstring (R.string. Share_failure), Toast.length_long); }    };}

Source: Chengdu App Development www.cxb360.com

Share a complete set of Android share function codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.