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.
- Private IWXAPI wxApi;
- // Instantiate
- WxApi = wxapifacloud. createWXAPI (this, Constants. WX_APP_ID );
- WxApi. registerApp (Constants. WX_APP_ID );
- /**
- * Share (only one example of sharing web pages is provided here. For more information, see the sample code on the official website)
- * @ Param flag (0: Share with friends, 1: Share with friends)
- */
- Private void wechatShare (int flag ){
- WXWebpageObject webpage = new WXWebpageObject ();
- Webpage. webpageUrl = "Enter the url here ";
- WXMediaMessage msg = new WXMediaMessage (webpage );
- Msg. title = "Enter the title here ";
- Msg. description = "Enter content here ";
- // Replace the image resource in your project
- Bitmap thumb = BitmapFactory. decodeResource (getResources (), R. drawable. demo_logo );
- Msg. setThumbImage (thumb );
- SendMessageToWX. Req req = new SendMessageToWX. Req ();
- Req. transaction = String. valueOf (System. currentTimeMillis ());
- Req. message = msg;
- Req. scene = flag = 0? SendMessageToWX. Req. WXSceneSession: SendMessageToWX. Req. WXSceneTimeline;
- WxApi. sendReq (req );
- }
- // Add the Code where you want to share it:
- WechatShare (0); // share with friends
- 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.
- /** Example of client callback activity */
- Public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
- // IWXAPI is an openapi for third-party apps and communications
- Private IWXAPI api;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Api = wxapifacloud. createWXAPI (this, "Replace the APP_ID applied in step 1", false );
- Api. handleIntent (getIntent (), this );
- Super. onCreate (savedInstanceState );
- }
- @ Override
- Public void onReq (BaseReq arg0 ){}
- @ Override
- Public void onResp (BaseResp resp ){
- LogManager. show (TAG, "resp. errCode:" + resp. errCode + ", resp. errStr :"
- + Resp. errStr, 1 );
- Switch (resp. errCode ){
- Case BaseResp. ErrCode. ERR_ OK:
- // Shared successfully
- Break;
- Case BaseResp. ErrCode. ERR_USER_CANCEL:
- // Cancel sharing
- Break;
- Case BaseResp. ErrCode. ERR_AUTH_DENIED:
- // Share rejected
- Break;
- }
- }
- }
Vi. Configuration list file AndroidManifest. xml
- <Activity
- Android: name = ". wxapi. WXEntryActivity"
- Android: exported = "true"
- Android: screenOrientation = "portrait"
- Android: theme = "@ android: style/Theme. Translucent. NoTitleBar"/>
7. The signature packaging software can be tested.