今天就跟大家聊聊有關(guān)Android應(yīng)用中怎么接入微信分享功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、門(mén)源網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為門(mén)源等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
注冊(cè)應(yīng)用信息并下載必要工具
在微信開(kāi)放平臺(tái)注冊(cè)應(yīng)用信息
下載 簽名生成工具獲取待接入應(yīng)用的簽名
應(yīng)用信息填入簽名
注意:由于一般調(diào)試簽名和正式簽名不一致,測(cè)試時(shí)可填入測(cè)試包的簽名,上線時(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來(lái)處理微信分享結(jié)果回調(diào)
主題:
Activity
當(dāng)微信分享成功,點(diǎn)擊返回App時(shí),微信會(huì)主動(dòng)調(diào)用WXEntryActivity,并將結(jié)果通過(guò)Intent傳給WXEntryActivity,此時(shí)調(diào)用api.handleIntent(getIntent(), this)對(duì)結(jié)果進(jìn)行處理,handleIntent第二個(gè)參數(shù)為IWXAPIEventHandler,分享結(jié)果會(huì)在onResp方法中處理,在這里我們發(fā)送一個(gè)廣播,并在WXShare對(duì)象中對(duì)該廣播進(jìn)行監(jiān)聽(tīng)
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(); //注意: //第三方開(kāi)發(fā)者如果使用透明界面來(lái)實(shí)現(xiàn)WXEntryActivity,需要判斷handleIntent的返回值,如果返回值為false,則說(shuō)明入?yún)⒉缓戏ㄎ幢籗DK處理,應(yīng)finish當(dāng)前透明界面,避免外部通過(guò)傳遞非法參數(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(); }
啟動(dòng)分享
wxShare.share("這是要分享的文字");
到這里微信分享就完成啦!
另外,在微信官方例程中,有個(gè)定時(shí)刷新應(yīng)用注冊(cè)信息的receiver
AndroidManifest.xml中添加:
代碼:
public class AppRegister extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final IWXAPI api = WXAPIFactory.createWXAPI(context, null); // 將該app注冊(cè)到微信 api.registerApp(WXShare.APP_ID); } }
看完上述內(nèi)容,你們對(duì)Android應(yīng)用中怎么接入微信分享功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。