第一步:去微信開放平臺(https://open.weixin.qq.com/)建立應用,並拿到AppId。java
第二步:導入微信支持jar文件android
。。。這裏假設讀者已經瞭解通常的第三方API開發,一些步驟就省略了。web
第三步:發送圖文消息到微信朋友圈(按鈕觸發事件里加入如下代碼,請確保應用簽名與在開放平臺註冊的應用簽名一致,且AppId也應該對應)api
IWXAPI api = WXAPIFactory.createWXAPI(context, APP_ID, false); api.registerApp(APP_ID); WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = "http://www.xxxx.com/wap/showShare/;//收到分享的好友點擊會跳轉到這個地址裏面去 WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = "我要約"; msg.description = "我要約分享"; try { Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.setThumbImage(thumbBmp); } catch (Exception e) { e.printStackTrace(); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis()); req.message = msg; req.scene = SendMessageToWX.Req.WXSceneTimeline; api.sendReq(req);
若是一切順利,就會看到了文章開頭相似的圖片。微信
這時點擊取消或分享沒有任何提示。app
第四步:加提示回調頁面。ide
(1)新建一個包this
規則是你的程序包名加wxapi【例如:com.xxx.wxapi】spa
(2)在新建的包裏面添加一個類,WXEntryActivity.java,名字必須一致,這是微信開放平臺規定的。code
package com.xxx.wxapi; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.lbt.hairdesigner.R; import com.lbt.hairdesigner.utils.MySetting; import com.tencent.mm.sdk.openapi.BaseReq; import com.tencent.mm.sdk.openapi.BaseResp; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.IWXAPIEventHandler; import com.tencent.mm.sdk.openapi.WXAPIFactory; public class WXEntryActivity extends Activity implements IWXAPIEventHandler { private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, APP_ID, false); api.registerApp(APP_ID); api.handleIntent(getIntent(), this); } @Override public void onReq(BaseReq req) { } @Override public void onResp(BaseResp resp) { int result = 0; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = R.string.errcode_success; break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = R.string.errcode_cancel; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = R.string.errcode_deny; break; default: result = R.string.errcode_unknown; break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); overridePendingTransition(R.anim.change_in, R.anim.change_out); } }
(3)string.xml中添加一些字符item
<string name="errcode_success">發送成功</string> <string name="errcode_cancel">發送取消</string> <string name="errcode_deny">發送被拒絕</string> <string name="errcode_unknown">發送返回</string>
(4)附上change_in.xml 和change_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" > <scale android:duration="@android:integer/config_shortAnimTime" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%p" android:pivotY="50%p" android:toXScale="1.0" android:toYScale="1.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:zAdjustment="top" > <scale android:duration="@android:integer/config_shortAnimTime" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%p" android:pivotY="50%p" android:toXScale="1.0" android:toYScale="1.0" /> <alpha android:duration="@android:integer/config_shortAnimTime" android:fromAlpha="1.0" android:toAlpha=".0" /> </set>
最後:無論你在哪一個地方調用第三步分享,微信都會有回調提示了。