In recent projects, you need to connect to WeChat to share and record the access process
Register the application information and download the necessary tools
- existWeChat Open PlatformRegister application information
- downloadSignature generation toolGet the signature of the application to be accessed
- Fill in the signature for application information
Note: Since the general debugging signature and formal signature are inconsistent, the signature of the test package can be filled in during testing. When it is launched, it needs to be changed to the signature of the official package.
Access
In, add the following dependencies:
dependencies { compile ':wechat-sdk-android-with-mta:+' }
or:
dependencies { compile ':wechat-sdk-android-without-mta:+' }
Among them, the former contains statistical functions
Add permissions and WeChat callback Activity
<uses-permission android:name="" /> <uses-permission android:name=".MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/> <!-- for mta statistics --> <uses-permission android:name=".ACCESS_WIFI_STATE"/> <uses-permission android:name=".READ_PHONE_STATE"/> <uses-permission android:name=".ACCESS_NETWORK_STATE"/> <activity android:name=".WXEntryActivity" android:exported="true" android:screenOrientation="portrait" android:theme="@style/"/>
Here is a transparent activity to handle WeChat share result callbacks
theme:
<style name="" parent=""> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item> </style> <style name=""> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/</item> </style>
Activity
When WeChat shares successfully and click to return to the App, WeChat will actively call WXEntryActivity and pass the result to WXEntryActivity through Intent. At this time, (getIntent(), this) is called to process the result. The second parameter of handleIntent is IWXAPIEventHandler. The sharing result will be processed in the onResp method. Here we send a broadcast and listen to the broadcast in the WXShare object.
public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler { private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); ("WXEntryActivity"); WXShare share = new WXShare(this); api = share // .register() .getApi(); //Notice: //If a third-party developer uses a transparent interface to implement WXEntryActivity, he needs to judge the return value of the handleIntent. If the return value is false, it means that the entry parameter is illegal and has not been processed by the SDK. The current transparent interface should be finished to avoid the external intent passing illegal parameters causing it to stay in the transparent interface, causing users to be confused by the user try { if (!(getIntent(), this)) { finish(); } } catch (Exception e) { (); } } @Override protected void onNewIntent(Intent intent) { (intent); ("onNewIntent"); setIntent(intent); if (!(intent, this)) { finish(); } } @Override public void onReq(BaseReq baseReq) { } @Override public void onResp(BaseResp baseResp) { Intent intent = new Intent(WXShare.ACTION_SHARE_RESPONSE); (WXShare.EXTRA_RESULT, new (baseResp)); sendBroadcast(intent); finish(); } }
Tools
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 = (context, APP_ID); = context; } public WXShare register() { // Share on WeChat (APP_ID); receiver = new ResponseReceiver(); IntentFilter filter = new IntentFilter(ACTION_SHARE_RESPONSE); (receiver, filter); return this; } public void unregister() { try { (); (receiver); } catch (Exception e) { (); } } public WXShare share(String text) { WXTextObject textObj = new WXTextObject(); = text; WXMediaMessage msg = new WXMediaMessage(); = textObj; // = "Will be ignored"; = text; req = new (); = buildTransaction("text"); = msg; = ; boolean result = (req); ("text shared: " + result); return this; } public IWXAPI getApi() { return api; } public void setListener(OnResponseListener listener) { = listener; } private String buildTransaction(final String type) { return (type == null) ? (()) : type + (); } private class ResponseReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Response response = (EXTRA_RESULT); ("type: " + ()); ("errCode: " + ); String result; if (listener != null) { if ( == .ERR_OK) { (); } else if ( == .ERR_USER_CANCEL) { (); } else { switch () { case .ERR_AUTH_DENIED: result = "Send Denied"; break; case .ERR_UNSUPPORT: result = "Not supported error"; break; default: result = "Send Return"; break; } (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 = ; errStr = ; transaction = ; openId = ; type = (); checkResult = (); } @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) { (); (); (); (); (); ( ? (byte) 1 : (byte) 0); } protected Response(Parcel in) { = (); = (); = (); = (); = (); = () != 0; } public static final Creator<Response> CREATOR = new Creator<Response>() { @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 activities that need to be shared using WeChat:
@Override protected void onCreate(Bundle savedInstanceState) { wxShare = new WXShare(this); (new OnResponseListener() { @Override public void onSuccess() { // Share successfully } @Override public void onCancel() { // Share Cancel } @Override public void onFail(String message) { // Sharing failed } }); } @Override protected void onStart() { (); (); } @Override protected void onDestroy() { (); (); }
Start sharing
("This is the text to share");
Here you can share on WeChat!
In addition,WeChat official routineIn this case, there is a receiver that refreshes the application registration information regularly
Added in:
<receiver android:name=".AppRegister" android:permission=""> <intent-filter> <action android:name=".ACTION_REFRESH_WXAPP"/> </intent-filter> </receiver>
Code:
public class AppRegister extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final IWXAPI api = (context, null); // Register the app to WeChat (WXShare.APP_ID); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.