In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
The editor of this article introduces in detail "how to access Wechat sharing function in Android application". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to access Wechat sharing function in Android application" can help you solve your doubts.
Register the application information and download the necessary tools
Register application information on Wechat open platform
Download the signature generation tool to obtain the signature of the application to be accessed
Fill in the signature with application information
Note: due to the discrepancy between the general debug signature and the official signature, the signature of the test package can be filled in during the test, and it needs to be changed to the signature of the official package when launching.
Access
In build.gradle, add the following dependencies:
Dependencies {compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'}
Or:
Dependencies {compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'}
Among them, the former contains statistical function
Add permissions to AndroidManifest.xml and Wechat callback Activity
A transparent Activity is used to handle the callback of Wechat sharing results.
Themes:
True @ android:color/transparent true @ android:style/Animation.Translucent
Activity
When Wechat shares successfully and clicks to return to App, Wechat will actively call WXEntryActivity and pass the result to WXEntryActivity through Intent. At this time, call api.handleIntent (getIntent (), this) to handle the result. The second parameter of handleIntent is IWXAPIEventHandler, and the sharing result will be handled in the onResp method. Here we send a broadcast and listen to it in the WXShare object.
Public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler {private IWXAPI api; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); Logger.i ("WXEntryActivity"); WXShare share = new WXShare (this); api = share / / .register () .getApi () / / Note: / / third-party developers need to judge the return value of handleIntent if they use a transparent interface to implement WXEntryActivity. If the return value is false, the input parameter is illegal and has not been handled by SDK. The current transparent interface should be finish to prevent the external Intent from staying in the transparent interface by passing illegal parameters. Cause confusion to users try {if (! api.handleIntent (getIntent (), this)) {finish () }} catch (Exception e) {e.printStackTrace ();}} @ Override protected void onNewIntent (Intent intent) {super.onNewIntent (intent); Logger.i ("onNewIntent"); setIntent (intent); if (! api.handleIntent (intent, this)) {finish () } @ Override public void onReq (BaseReq baseReq) {} @ Override public void onResp (BaseResp baseResp) {Intent intent = new Intent (WXShare.ACTION_SHARE_RESPONSE); intent.putExtra (WXShare.EXTRA_RESULT, new WXShare.Response (baseResp)); sendBroadcast (intent); finish ();}}
Tool class
Public class WXShare {public static final String APP_ID = "wx0123456789"; public static final String ACTION_SHARE_RESPONSE = "action_wx_share_response"; public static final String EXTRA_RESULT = "result"; private final Context context; private final IWXAPI api; private OnResponseListener listener; private ResponseReceiver receiver; public WXShare (Context context) {api = WXAPIFactory.createWXAPI (context, APP_ID); this.context = context } public WXShare register () {/ / Wechat sharing api.registerApp (APP_ID); receiver = new ResponseReceiver (); IntentFilter filter = new IntentFilter (ACTION_SHARE_RESPONSE); context.registerReceiver (receiver, filter); return this;} public void unregister () {try {api.unregisterApp (); context.unregisterReceiver (receiver) } catch (Exception e) {e.printStackTrace ();}} public WXShare share (String text) {WXTextObject textObj = new WXTextObject (); textObj.text = text; WXMediaMessage msg = new WXMediaMessage (); msg.mediaObject = textObj; / / msg.title = "Will be ignored"; msg.description = text; SendMessageToWX.Req req = new SendMessageToWX.Req (); req.transaction = buildTransaction ("text") Req.message = msg; req.scene = SendMessageToWX.Req.WXSceneSession; boolean result = api.sendReq (req); Logger.i ("text shared:" + result); return this;} public IWXAPI getApi () {return api;} public void setListener (OnResponseListener listener) {this.listener = listener;} private String buildTransaction (final String type) {return (type = null)? String.valueOf (System.currentTimeMillis ()): type + System.currentTimeMillis ();} private class ResponseReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {Response response = intent.getParcelableExtra (EXTRA_RESULT); Logger.d ("type:" + response.getType ()); Logger.d ("errCode:" + response.errCode); String result If (listener! = null) {if (response.errCode = = BaseResp.ErrCode.ERR_OK) {listener.onSuccess ();} else if (response.errCode = = BaseResp.ErrCode.ERR_USER_CANCEL) {listener.onCancel () } else {switch (response.errCode) {case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "send rejected"; break; case BaseResp.ErrCode.ERR_UNSUPPORT: result = "unsupported error"; break Default: result = "send return"; break;} listener.onFail (result);}} public static class Response extends BaseResp implements Parcelable {public int errCode; public String errStr; public String transaction; public String openId; private int type; private boolean checkResult Public Response (BaseResp baseResp) {errCode = baseResp.errCode; errStr = baseResp.errStr; transaction = baseResp.transaction; openId = baseResp.openId; type = baseResp.getType (); checkResult = baseResp.checkArgs ();} @ Override public int getType () {return type;} @ Override public boolean checkArgs () {return checkResult } @ Override public int describeContents () {return 0;} @ Override public void writeToParcel (Parcel dest, int flags) {dest.writeInt (this.errCode); dest.writeString (this.errStr); dest.writeString (this.transaction); dest.writeString (this.openId); dest.writeInt (this.type); dest.writeByte (this.checkResult? (byte) 1: (byte) 0;} protected Response (Parcel in) {this.errCode = in.readInt (); this.errStr = in.readString (); this.transaction = in.readString (); this.openId = in.readString (); this.type = in.readInt (); this.checkResult = in.readByte ()! = 0 } public static final Creator CREATOR = new Creator () {@ Override public Response createFromParcel (Parcel source) {return new Response (source);} @ Override public Response [] newArray (int size) {return new Response [size];};}}
Interface
Public interface OnResponseListener {void onSuccess (); void onCancel (); void onFail (String message);}
Use
In the Activity that needs to be shared with Wechat:
@ Overrideprotected void onCreate (Bundle savedInstanceState) {wxShare = new WXShare (this); wxShare.setListener (new OnResponseListener () {@ Override public void onSuccess () {/ / sharing success} @ Override public void onCancel () {/ / sharing cancellation} @ Override public void onFail (String message) {/ / sharing failure}});} @ Overrideprotected void onStart () {super.onStart () WxShare.register ();} @ Overrideprotected void onDestroy () {wxShare.unregister (); super.onDestroy ();}
Start sharing
WxShare.share ("this is the text to be shared")
This is where Wechat sharing is complete!
In addition, in the official routine of Wechat, there is a receiver that regularly refreshes the application registration information.
Add to AndroidManifest.xml:
Code:
Public class AppRegister extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {final IWXAPI api = WXAPIFactory.createWXAPI (context, null); / / Register the app to Wechat api.registerApp (WXShare.APP_ID) }} after reading this, the article "how to access Wechat sharing function in Android App" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.