- 準(zhǔn)備材料:
- 微信開(kāi)發(fā)者賬號(hào)
- 注冊(cè)你的APP
- libammsdk.jar包
- debug.keystore文件
準(zhǔn)備工作
申請(qǐng)你的APPID
導(dǎo)入libammsdk.jar包
-
在AndroidManifest.xml中設(shè)置權(quán)限
<uses-permission android:name="android.permission.INTERNET"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-
注冊(cè)APP到微信
private static final String App_Id = "你注冊(cè)的APP_ID"; private IWXAPI api; public void registerToWX(){ api = WXAPIFactory.createWXAPI(this, App_Id, true); api.registerApp(App_ID); }
分享:
-
分享(以分享圖片為例)
直接觸發(fā)分享圖片按鈕的點(diǎn)擊事件findViewById(R.id.send_img).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img); WXImageObject imgObj = new WXImageObject(bmp); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("img"); req.message = msg; req.scene = SendMessageToWX.Req.WXSceneTimeline; SendMessageToWX.Req.WXSceneSession; api.sendReq(req); finish(); } });
其中req.scene表示分享選擇分別代表分享到朋友圈和會(huì)話框
Util是一個(gè)圖片處理的類
登錄
-
登錄需要重新APP到微信吗伤,需要觸發(fā)回調(diào),下面直接上代碼:
package com.lht.pan_android.wxapi; import org.apache.http.Header; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.alibaba.fastjson.JSON; import com.lht.pan_android.HttpUtil; import com.lht.pan_android.MainActivity; import com.lht.pan_android.R; import com.lht.pan_android.WeChatCodeBean; import com.loopj.android.http.AsyncHttpResponseHandler; import com.tencent.mm.sdk.modelbase.BaseReq; import com.tencent.mm.sdk.modelbase.BaseResp; import com.tencent.mm.sdk.modelmsg.SendAuth; 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 static final String TAG = "wechat"; private static final String APP_ID = "你的APP_Id"; private static final String APP_SECRET = "你的APP_secret"; private Button registerBtn; private IWXAPI api; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.entry); registerBtn = (Button) findViewById(R.id.reg_btn); tv = (TextView) findViewById(R.id.log); api = WXAPIFactory.createWXAPI(this, APP_ID, true); registerBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { loginWithWeixin(); } }); api.handleIntent(getIntent(), this); } //登錄信息 private void loginWithWeixin() { if (api == null) { api = WXAPIFactory.createWXAPI(this, APP_ID, false); } if (!api.isWXAppInstalled()) { Toast.makeText(this, "沒(méi)有安裝微信", Toast.LENGTH_SHORT).show(); return; } api.registerApp(APP_ID); SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "com.lht.bridge.session"; api.sendReq(req); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } //實(shí)現(xiàn)了IWXAPIEventHandler @Override public void onReq(BaseReq req) { } //實(shí)現(xiàn)了IWXAPIEventHandler @Override public void onResp(BaseResp resp) { code_code = ((SendAuth.Resp) resp).code; String result = null; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = "發(fā)送成功"; //此處進(jìn)行數(shù)據(jù)請(qǐng)求损合,請(qǐng)求用戶信息 Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "發(fā)送取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "發(fā)送被拒絕"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "發(fā)送返回"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } }
實(shí)現(xiàn)IWXAPIEventHandler接口,微信發(fā)送的請(qǐng)求將回調(diào)到onReq方法末患,發(fā)送到微信請(qǐng)求的響應(yīng)結(jié)果將回調(diào)到onResp方法
在WXEntryActivity中將接收到的intent及實(shí)現(xiàn)了IWXAPIEventHandler接口的對(duì)象傳遞給IWXAPI接口的handleIntent方法
- 在Androidmanifest.xml中注冊(cè):
<activity
android:name = ".wxapi.WXEntryActivity"
android:exported = "true"/>
- 請(qǐng)求用戶信息:
微信需要先獲取到code驹溃,再根據(jù)code獲取access_token,根據(jù)access_token獲取openId每庆,最后根據(jù)access_token和openId獲取用戶信息
到此微信登錄和分享的流程基本走通了值纱,但是還有許多需要注意的問(wèn)題
- 1、最最最重要的一點(diǎn)是登錄需要單獨(dú)寫(xiě)一個(gè)包,據(jù)說(shuō)這樣登錄才能走微信自己的一套機(jī)制
格式為:包名.wxapi 類名:WXEntryActivity.java - 2蘸嘶、導(dǎo)入的jar包問(wèn)題良瞧,jar包導(dǎo)入的不對(duì)就可能導(dǎo)致獲取code這一步出現(xiàn)問(wèn)題,微信的jar包分為老版本和新版本训唱,新版本的可以直接獲取access_token褥蚯,但是官網(wǎng)并沒(méi)有說(shuō)明怎么通過(guò)access_token獲取用戶信息,老版本的jar包是可以獲取code的况增,然后自己根據(jù)code自己寫(xiě)網(wǎng)絡(luò)請(qǐng)求(你沒(méi)有看錯(cuò)赞庶,就是自己寫(xiě)網(wǎng)絡(luò)請(qǐng)求);
- 3、程序運(yùn)行時(shí)一定要保證你的簽名正確歧强,如果需要更換你的key澜薄,windows下需要更換sdk/.android/debug.keystore以正確替換你的key
目前能想到的就這些了,有什么問(wèn)題歡迎指出摊册,最后吐槽一下微信登錄竟然收費(fèi)肤京、微信的文檔就呵呵了