A simple method of adding micro-credit sharing to Android applications _android

Source: Internet
Author: User

First, apply for your AppID
http://open.weixin.qq.com/

Friendly tip: It is recommended to apply the MD5 value of the last step of the Eclipse Packaging software application AppID

Second, the official website download Libammsdk.jar Package
Http://open.weixin.qq.com/download/?lang=zh_CN

Iii. Copy the Libammsdk.jar to the Libs directory of the Project

Iv. writing code in activity that needs to be shared

Copy Code code as follows:

Private Iwxapi Wxapi;
Instantiation of
Wxapi = Wxapifactory.createwxapi (this, constants.wx_app_id);
Wxapi.registerapp (constants.wx_app_id);
/**
* Micro-credit sharing (only one example of sharing the Web page, please refer to the official website sample code)
* @param flag (0: Share to micro-trust friends, 1: Share to micro-trust friends Circle)
*/
private void Wechatshare (int flag) {
Wxwebpageobject webpage = new Wxwebpageobject ();
Webpage.webpageurl = "Fill in the link URL here";
Wxmediamessage msg = new Wxmediamessage (webpage);
Msg.title = "Fill in the title here";
Msg.description = "Fill in the content here";
Here, replace a picture resource in your own project.
Bitmap thumb = Bitmapfactory.decoderesource (Getresources (), R.drawable.share_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 code where you need to share:
Wechatshare (0)//share to micro-trust friends
Wechatshare (1)//share to the micro-trust Friends Circle

Create a new WXAPI directory under your package name and add a wxentryactivity class to the Wxapi directory, which inherits from the activity and implements the Iwxapieventhandler interface

Copy Code code as follows:

/** micro-Credit Client callback activity sample * *
public class Wxentryactivity extends activity implements Iwxapieventhandler {
IWXAPI is the Openapi interface for third-party app and micro-communication
Private IWXAPI API;
@Override
protected void OnCreate (Bundle savedinstancestate) {
API = Wxapifactory.createwxapi (this, "replace the app_id that the first step applies", 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:
Share success
Break
Case BaseResp.ErrCode.ERR_USER_CANCEL:
Share cancellation
Break
Case BaseResp.ErrCode.ERR_AUTH_DENIED:
Share rejection
Break
}
}
}

VI. Configuration manifest file Androidmanifest.xml

Copy Code code as follows:

<activity
Android:name= ". Wxapi. Wxentryactivity "
Android:exported= "true"
android:screenorientation= "Portrait"
Android:theme= "@android: Style/theme.translucent.notitlebar"/>

Seven, signed packaging software, and then you can test the

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.