本文小編為大家詳細(xì)介紹“Android應(yīng)用中如何接入微信分享功能”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Android應(yīng)用中如何接入微信分享功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元平桂做網(wǎng)站,已為上家服務(wù),為平桂各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
注冊應(yīng)用信息并下載必要工具
在微信開放平臺注冊應(yīng)用信息
下載 簽名生成工具獲取待接入應(yīng)用的簽名
應(yīng)用信息填入簽名
注意:由于一般調(diào)試簽名和正式簽名不一致,測試時(shí)可填入測試包的簽名,上線時(shí)需要改為正式包的簽名
接入
在build.gradle中,添加如下依賴:
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' }
或:
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' }
其中,前者包含統(tǒng)計(jì)功能
AndroidManifest.xml中添加權(quán)限和微信回調(diào)Activity
這里使用了一個(gè)透明的Activity來處理微信分享結(jié)果回調(diào)
主題:
Activity
當(dāng)微信分享成功,點(diǎn)擊返回App時(shí),微信會主動調(diào)用WXEntryActivity,并將結(jié)果通過Intent傳給WXEntryActivity,此時(shí)調(diào)用api.handleIntent(getIntent(), this)對結(jié)果進(jìn)行處理,handleIntent第二個(gè)參數(shù)為IWXAPIEventHandler,分享結(jié)果會在onResp方法中處理,在這里我們發(fā)送一個(gè)廣播,并在WXShare對象中對該廣播進(jìn)行監(jiān)聽
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(); //注意: //第三方開發(fā)者如果使用透明界面來實(shí)現(xiàn)WXEntryActivity,需要判斷handleIntent的返回值,如果返回值為false,則說明入?yún)⒉缓戏ㄎ幢籗DK處理,應(yīng)finish當(dāng)前透明界面,避免外部通過傳遞非法參數(shù)的Intent導(dǎo)致停留在透明界面,引起用戶的疑惑 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(); } }
工具類
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() { // 微信分享 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 = "發(fā)送被拒絕"; break; case BaseResp.ErrCode.ERR_UNSUPPORT: result = "不支持錯(cuò)誤"; break; default: result = "發(fā)送返回"; 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 CreatorCREATOR = new Creator () { @Override public Response createFromParcel(Parcel source) { return new Response(source); } @Override public Response[] newArray(int size) { return new Response[size]; } }; } }
接口
public interface OnResponseListener { void onSuccess(); void onCancel(); void onFail(String message); }
使用
在需要使用微信分享的Activity中:
@Override protected void onCreate(Bundle savedInstanceState) { wxShare = new WXShare(this); wxShare.setListener(new OnResponseListener() { @Override public void onSuccess() { // 分享成功 } @Override public void onCancel() { // 分享取消 } @Override public void onFail(String message) { // 分享失敗 } }); } @Override protected void onStart() { super.onStart(); wxShare.register(); } @Override protected void onDestroy() { wxShare.unregister(); super.onDestroy(); }
啟動分享
wxShare.share("這是要分享的文字");
到這里微信分享就完成啦!
另外,在微信官方例程中,有個(gè)定時(shí)刷新應(yīng)用注冊信息的receiver
AndroidManifest.xml中添加:
代碼:
public class AppRegister extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final IWXAPI api = WXAPIFactory.createWXAPI(context, null); // 將該app注冊到微信 api.registerApp(WXShare.APP_ID); } }
讀到這里,這篇“Android應(yīng)用中如何接入微信分享功能”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。