最近在作android端分享的功能,在微信開放平臺查看了下官網上的開發文檔,一步一步的按文檔上的步驟來:android
一切都挺順利的,調試了也能正常發送分享。可是當發佈後再安裝到手機上,點擊分享的時候就沒有任何反應,也沒提示。api
最後在網上查到說應用包名、簽名、APP_ID要對應,想一想問題應該出如今簽名上,就安裝了發佈後的apk,再用genSignature.apk獲取了一下簽名,微信
發現和原來的不同。因而用這個簽名又去官網登記了一個應用,獲取新的appid,appkey。發佈的時候使用剛登記的appid,安裝後微信分享沒問題了,微信開發
大功告成。app
因此作微信分享的時候要申請兩個應用,一個用作開發,一個用在發佈正式上。ide
分享一下代碼(替換本身的appid)工具
public class WXEntryActivity extends Activity implements IWXAPIEventHandler { private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LogUtil.d("微信", "微信響應頁面", true); api = WXAPIFactory.createWXAPI(this, AppID, false); api.registerApp(AppID); api.handleIntent(getIntent(), this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } public void onReq(BaseReq arg0) { LogUtil.d("微信", "BaseReq:" + arg0.getType(), true); switch (arg0.getType()) { case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX: LogUtil.d("", "ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX", true); break; case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX: LogUtil.d("", "ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX", true); break; default: break; } finish(); } public void onResp(BaseResp arg0) { LogUtil.d("微信", "BaseResp:" + arg0.errCode, true); String result = ""; switch (arg0.errCode) { case BaseResp.ErrCode.ERR_OK: result = "親,分享成功了"; break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "取消分享"; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "認證失敗"; break; default: result = "errcode_unknown"; break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); } }
分享方法開發工具
IWXAPI wxApi = WXAPIFactory.createWXAPI(this, AppID); wxApi.registerApp(AppID);
public void WxSendRequest(int towhere, String sharestr) { LogUtil.e("", "博餅微信分享", true); WXTextObject txtObject = new WXTextObject(); txtObject.text = sharestr; WXMediaMessage msg = new WXMediaMessage(txtObject); msg.title = mBoCake.getTitle(); msg.description = mBoCake.getTitle(); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis()); req.message = msg; if (towhere == 0) { req.scene = SendMessageToWX.Req.WXSceneSession;//分享給好友 } else { req.scene = SendMessageToWX.Req.WXSceneTimeline;//朋友圈 } wxApi.sendReq(req); }