Add Android apps to WeChat sharing

Source: Internet
Author: User

1. Apply for your AppID

Http://open.weixin.qq.com/

Tip: We recommend that you use the MD5 value of the last step of eclipse packaging software to apply for AppID.

2. Download The libammsdk. jar package from the official website.

Http://open.weixin.qq.com/download? Lang = zh_CN

3. Copy libammsdk. jar to the libs directory of the project. 4. write code in the Activity to be shared.
  1. Private IWXAPI wxApi;
  2. // Instantiate
  3. WxApi = wxapifacloud. createWXAPI (this, Constants. WX_APP_ID );
  4. WxApi. registerApp (Constants. WX_APP_ID );
  5. /**
  6. * Share (only one example of sharing web pages is provided here. For more information, see the sample code on the official website)
  7. * @ Param flag (0: Share with friends, 1: Share with friends)
  8. */
  9. Private void wechatShare (int flag ){
  10. WXWebpageObject webpage = new WXWebpageObject ();
  11. Webpage. webpageUrl = "Enter the url here ";
  12. WXMediaMessage msg = new WXMediaMessage (webpage );
  13. Msg. title = "Enter the title here ";
  14. Msg. description = "Enter content here ";
  15. // Replace the image resource in your project
  16. Bitmap thumb = BitmapFactory. decodeResource (getResources (), R. drawable. demo_logo );
  17. Msg. setThumbImage (thumb );
  18. SendMessageToWX. Req req = new SendMessageToWX. Req ();
  19. Req. transaction = String. valueOf (System. currentTimeMillis ());
  20. Req. message = msg;
  21. Req. scene = flag = 0? SendMessageToWX. Req. WXSceneSession: SendMessageToWX. Req. WXSceneTimeline;
  22. WxApi. sendReq (req );
  23. }
  24. // Add the Code where you want to share it:
  25. WechatShare (0); // share with friends
  26. WechatShare (1); // share to your friends

 

5. Create a wxapi directory under the corresponding directory of your package name, and add a WXEntryActivity class under the wxapi directory. This class inherits from Activity and implements the IWXAPIEventHandler interface.
  1. /** Example of client callback activity */
  2. Public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
  3. // IWXAPI is an openapi for third-party apps and communications
  4. Private IWXAPI api;
  5. @ Override
  6. Protected void onCreate (Bundle savedInstanceState ){
  7. Api = wxapifacloud. createWXAPI (this, "Replace the APP_ID applied in step 1", false );
  8. Api. handleIntent (getIntent (), this );
  9. Super. onCreate (savedInstanceState );
  10. }
  11. @ Override
  12. Public void onReq (BaseReq arg0 ){}
  13. @ Override
  14. Public void onResp (BaseResp resp ){
  15. LogManager. show (TAG, "resp. errCode:" + resp. errCode + ", resp. errStr :"
  16. + Resp. errStr, 1 );
  17. Switch (resp. errCode ){
  18. Case BaseResp. ErrCode. ERR_ OK:
  19. // Shared successfully
  20. Break;
  21. Case BaseResp. ErrCode. ERR_USER_CANCEL:
  22. // Cancel sharing
  23. Break;
  24. Case BaseResp. ErrCode. ERR_AUTH_DENIED:
  25. // Share rejected
  26. Break;
  27. }
  28. }
  29. }
Vi. Configuration list file AndroidManifest. xml
  1. <Activity
  2. Android: name = ". wxapi. WXEntryActivity"
  3. Android: exported = "true"
  4. Android: screenOrientation = "portrait"
  5. Android: theme = "@ android: style/Theme. Translucent. NoTitleBar"/>
7. The signature packaging software can be tested.

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.