Android Micro-trust SDK implementation sharing _android

Source: Internet
Author: User

Use the SDK provided by the micro-mail to achieve sharing:

Download the Android-related jar package from http://open.weixin.qq.com and add Libammsdk.jar to the project.

The core class of micro-credit sharing, part of the code is as follows:

Wechatsharemanager.java

Package com.jackie.umeng.share; 
Import Android.content.Context; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.util.Log; 
 
Import Android.widget.Toast; 
Import Com.tencent.mm.sdk.modelmsg.SendMessageToWX; 
Import Com.tencent.mm.sdk.modelmsg.WXImageObject; 
Import Com.tencent.mm.sdk.modelmsg.WXMediaMessage; 
Import Com.tencent.mm.sdk.modelmsg.WXTextObject; 
Import Com.tencent.mm.sdk.modelmsg.WXVideoObject; 
Import Com.tencent.mm.sdk.modelmsg.WXWebpageObject; 
Import Com.tencent.mm.sdk.openapi.IWXAPI; 
 
Import Com.tencent.mm.sdk.openapi.WXAPIFactory; /** * Implementation of the micro-credit sharing function of the core class * @author CHENGCJ1 * * * * * */public class Wechatsharemanager {private static final int thumb_ 
  
 SIZE = 150; public static final int wechat_share_way_text = 1; Text public static final int wechat_share_way_picture = 2; Picture public static final int wechat_share_way_webpage = 3; Link public static final int wechat_share_way_video = 4; Video public static FINAl int wechat_share_type_talk = SendMessageToWX.Req.WXSceneSession; Session public static final int wechat_share_type_frends = SendMessageToWX.Req.WXSceneTimeline; 
 Friends Circle private static Wechatsharemanager minstance; 
 Private Sharecontent Msharecontenttext, Msharecontentpicture, Msharecontentwebpag, Msharecontentvideo; 
 Private Iwxapi Mwxapi; 
  
 Private context Mcontext; 
  Private Wechatsharemanager {this.mcontext = context; 
 Initialization data//initialization micro-letter sharing Code Initwechatshare (context); /** * Get weixinsharemanager instance * not thread safe, operate in UI thread * @return/public static Wechatsharemanager Getinsta 
  nce {if (minstance = = null) {minstance = new Wechatsharemanager (context); 
 return minstance; } private void Initwechatshare {if (Mwxapi = = null) {Mwxapi = Wxapifactory.createwxapi (conte 
  XT, wechatshareutil.wechat_app_id, True); 
 } mwxapi.registerapp (wechatshareutil.wechat_app_id); }/** * TongOver micro-letter sharing * @param shareway sharing (text, pictures, links) * @param sharetype sharing type (Friends Circle, session)/public void Sharebywebchat (Sharecon Tent sharecontent, int sharetype) {switch (Sharecontent.getshareway ()) {Case Wechat_share_way_text:sharetext (s 
   Harecontent, Sharetype); 
  Break 
   Case Wechat_share_way_picture:sharepicture (sharecontent, Sharetype); 
  Break 
   Case Wechat_share_way_webpage:sharewebpage (sharecontent, Sharetype); 
  Break 
   Case Wechat_share_way_video:sharevideo (sharecontent, Sharetype); 
  Break 
  } private abstract class Sharecontent {protected abstract int getshareway (); 
  Protected abstract String getcontent (); 
  Protected abstract String getTitle (); 
  Protected abstract String GetURL (); 
 protected abstract int getpictureresource (); /** * Set the content of shared text * @author CHENGCJ1 * */public class Sharecontenttext extends Sharecontent {Priva 
   
  Te String content; 
 /** * Construction sharing Text class * @param text content shared by text  */Public Sharecontenttext (String content) {this.content = content; 
  @Override protected int Getshareway () {return wechat_share_way_text; 
  @Override protected String getcontent () {return content; 
  @Override protected String GetTitle () {return null; 
  @Override protected String GetURL () {return null; 
  @Override protected int Getpictureresource () {return-1; /* * Get text Sharing object/public sharecontent Getsharecontenttext (String content) {if Msharecontenttext = 
  NULL) {Msharecontenttext = new sharecontenttext (content); 
 Return (Sharecontenttext) Msharecontenttext; /** * Settings Share picture content * @author chengcj1 * */public class Sharecontentpicture extends Sharecontent {PR 
  ivate int pictureresource; 
  Public sharecontentpicture (int pictureresource) {this.pictureresource = Pictureresource; 
  } @Override protected int getshareway () { return wechat_share_way_picture; 
  @Override protected int Getpictureresource () {return pictureresource; 
  @Override protected String getcontent () {return null; 
  @Override protected String GetTitle () {return null; 
  @Override protected String GetURL () {return null; /* * Get picture Sharing object/public sharecontent getsharecontentpicture (int pictureresource) {if (msharecontent 
  Picture = = null) {msharecontentpicture = new sharecontentpicture (Pictureresource); 
 Return (sharecontentpicture) msharecontentpicture; /** * Set content to share links * @author chengcj1 * */public class Sharecontentwebpage extends Sharecontent {PR 
  Ivate String title; 
  Private String content; 
  Private String URL; 
  private int pictureresource; 
   Public Sharecontentwebpage (string title, string content, string url, int pictureresource) {this.title = title; 
   this.content = content; This.url = URL;
   This.pictureresource = Pictureresource; 
  @Override protected int Getshareway () {return wechat_share_way_webpage; 
  @Override protected String getcontent () {return content; 
  @Override protected String GetTitle () {return title; 
  @Override protected String GetURL () {return URL; 
  @Override protected int Getpictureresource () {return pictureresource; * * * get Web Sharing object/Public sharecontent Getsharecontentwebpag (string title, string content, string URL, in T Pictureresource {if (Msharecontentwebpag = = null) {Msharecontentwebpag = new Sharecontentwebpage (title, Conten 
  T, URL, pictureresource); 
 Return (sharecontentwebpage) Msharecontentwebpag; /** * Settings Share video content * @author CHENGCJ1 * * */public class Sharecontentvideo extends Sharecontent {priv 
  Ate String URL; 
  Public sharecontentvideo (String url) {this.url = URL; } @Override ProtectEd int Getshareway () {return wechat_share_way_video; 
  @Override protected String getcontent () {return null; 
  @Override protected String GetTitle () {return null; 
  @Override protected String GetURL () {return URL; 
  @Override protected int Getpictureresource () {return-1; /* * Get video sharing content/public sharecontent getsharecontentvideo (String URL) {if Msharecontentvideo = = Nu 
  ll) {Msharecontentvideo = new Sharecontentvideo (URL); 
 Return (Sharecontentvideo) Msharecontentvideo; * * * Share text/private void Sharetext (sharecontent sharecontent, int sharetype) {String Text = Shareconte 
  Nt.getcontent (); 
  Initializes a Wxtextobject object wxtextobject textobj = new Wxtextobject (); 
  Textobj.text = text; 
  Initializes a Wxmediamessage object with the Wxtextobject object wxmediamessage msg = new Wxmediamessage (); 
  Msg.mediaobject = Textobj; 
  Msg.description = text; Construct a Req sendmessagetowx.req reQ = new Sendmessagetowx.req (); 
  The transaction field is used to uniquely identify a request Req.transaction = Buildtransaction ("Textshare"); 
  Req.message = msg; Send the target scene, you can choose to send to the session wxscenesession or friend Circle Wxscenetimeline. 
  The default message is sent to the session. 
  Req.scene = Sharetype; 
 Mwxapi.sendreq (req); * * * Share picture/private void SharePicture (sharecontent sharecontent, int sharetype) {Bitmap Bitmap = Bitmap 
  Factory.decoderesource (Mcontext.getresources (), Sharecontent.getpictureresource ()); 
   
  Wximageobject imgobj = new Wximageobject (bitmap); 
  Wxmediamessage msg = new Wxmediamessage (); 
   
  Msg.mediaobject = imgobj; 
  Bitmap Thumbbitmap = Bitmap.createscaledbitmap (Bitmap, Thumb_size, Thumb_size, true); 
  Bitmap.recycle (); Msg.thumbdata = Util.bmptobytearray (Thumbbitmap, true); 
  Set thumbnail sendmessagetowx.req Req = new Sendmessagetowx.req (); 
  Req.transaction = Buildtransaction ("Imgshareappdata"); 
  Req.message = msg; 
  Req.scene = Sharetype; 
 Mwxapi.sendreq (req); * * * Share links/Private void Sharewebpage (sharecontent sharecontent, int sharetype) {wxwebpageobject webpage = new Wxwebpageobject (); 
  Webpage.webpageurl = Sharecontent.geturl (); 
  Wxmediamessage msg = new Wxmediamessage (webpage); 
  Msg.title = Sharecontent.gettitle (); 
   
  Msg.description = Sharecontent.getcontent (); 
  Bitmap thumb = Bitmapfactory.decoderesource (Mcontext.getresources (), Sharecontent.getpictureresource ()); 
  if (thumb = = null) {Toast.maketext (Mcontext, "Picture cannot be empty", Toast.length_short). Show (); 
  else {msg.thumbdata = Util.bmptobytearray (thumb, true); 
  } sendmessagetowx.req Req = new Sendmessagetowx.req (); 
  Req.transaction = buildtransaction ("webpage"); 
  Req.message = msg; 
  Req.scene = Sharetype; 
 Mwxapi.sendreq (req); * * * Share video/private void Sharevideo (sharecontent sharecontent, int sharetype) {Wxvideoobject = n 
  EW Wxvideoobject (); 
 
  Video.videourl = Sharecontent.geturl (); Wxmediamessage msg = new Wxmediamessage (video);
  Msg.title = Sharecontent.gettitle (); 
  Msg.description = Sharecontent.getcontent (); 
Bitmap thumb = Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.send_music_thumb); 
  Bitmapfactory.decodestream (New URL (Video.videourl). OpenStream ()); /** * In the process of testing this situation, there will be individual mobile phones can not be able to adjust the micro-letter client situation. 
   This is due to the fact that micro-letters limit the size of the thumbnail, title, description, and so on, so it is possible that the size exceeds the default range. * In general, thumbnails are more common than usual. 
   Title, description are text, generally not over. 
  * * Bitmap Thumbbitmap = Bitmap.createscaledbitmap (thumb, thumb_size, thumb_size, true); 
  Thumb.recycle (); 
   
  Msg.thumbdata = Util.bmptobytearray (Thumbbitmap, true); 
  Sendmessagetowx.req Req = new Sendmessagetowx.req (); 
  Req.transaction = Buildtransaction ("video"); 
  Req.message = msg; 
  Req.scene = Sharetype; 
 Mwxapi.sendreq (req); Private String buildtransaction (final String type) {return (type = null)? 
 String.valueof (System.currenttimemillis ()): Type + system.currenttimemillis (); } 
}

Mainactivity.java

Package com.jackie.umeng.share; 
Import Com.jackie.umeng.share.WechatShareManager.ShareContentPicture; 
Import Com.jackie.umeng.share.WechatShareManager.ShareContentText; 
 
Import Com.jackie.umeng.share.WechatShareManager.ShareContentVideo; 
Import android.app.Activity; 
Import Android.content.Context; 
Import Android.content.pm.PackageManager; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.Toast; public class Mainactivity extends activity implements Onclicklistener {private Button msharetext, Msharepicture, Mshare 
 Video; 
  
 Private Wechatsharemanager Msharemanager; 
 
 Private context Mcontext; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
   
  Setcontentview (R.layout.activity_main); 
  Msharetext = (Button) Findviewbyid (R.id.share_text); 
  Msharepicture = (Button) Findviewbyid (r.id.share_picture); MshareviDeo = (Button) Findviewbyid (R.id.share_video); 
  Msharetext.setonclicklistener (this); 
  Msharepicture.setonclicklistener (this); 
   
  Msharevideo.setonclicklistener (this); 
   
  Mcontext = this; 
 Msharemanager = Wechatsharemanager.getinstance (Mcontext); @Override public void OnClick (View v) {if (!iswebchatavaliable ()) {Toast.maketext (Mcontext), "Install micro-letters first", Toa St. 
   Length_long). Show (); 
  Return Switch (V.getid ()) {case R.id.share_text:sharecontenttext Msharecontenttext = (sharecontenttext) msharemanag 
   Er.getsharecontenttext ("micro-letter text sharing"); 
   Msharemanager.sharebywebchat (Msharecontenttext, wechatsharemanager.wechat_share_type_frends); 
  Break Case R.id.share_picture:sharecontentpicture msharecontentpicture = (sharecontentpicture) 
   Msharemanager.getsharecontentpicture (R.drawable.share); 
   Msharemanager.sharebywebchat (Msharecontentpicture, wechatsharemanager.wechat_share_type_frends); 
  Break Case R.id.share_video:sharecontentvideo Msharecontentvideo = (sharecontentvideo) msharemanager.getsharecontentvideo ("Http://baidu.hz.letv.com/kan/agSlT?") 
   fr=v.baidu.com/"); 
   Msharemanager.sharebywebchat (Msharecontentvideo, wechatsharemanager.wechat_share_type_frends); 
  Break 
  Default:break; } private Boolean iswebchatavaliable () {//detect if a micro-letter try {Getpackagemanager () is installed on the phone. Getpackageinfo ("com.t 
   ENCENT.MM ", packagemanager.get_activities); 
  return true; 
  catch (Exception e) {return false;  } 
 } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.