Android實現(xiàn)微信登錄的示例代碼
微信登錄的實現(xiàn)與qq登錄類似。不過微信登錄比較麻煩,需要拿到開發(fā)者資質(zhì)認(rèn)證,花300塊錢,然后應(yīng)用的話還得有官網(wǎng)之類的,就是比較繁瑣的前期準(zhǔn)備工作,如果在公司里,這些應(yīng)該都不是事,會有相關(guān)人提前準(zhǔn)備好。在這里我們已經(jīng)拿到了開發(fā)者認(rèn)證,并且申請到了微信登錄的授權(quán)。
現(xiàn)在直接介紹mob來實現(xiàn)微信登錄的代碼,并獲取微信的相關(guān)數(shù)據(jù),比較簡單。
一、布局界面布局界面只需要一個button來觸發(fā)授權(quán)就可以
<Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='微信登錄' />二、MainActivity.java
public class MainActivity extends Activity { private Button wxlogin; private Platform wx; @Override protected void onCreate(Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);wxlogin = (Button) login_view.findViewById(R.id.wxlogin);wxlogin.setOnClickListener(new wxloginListener());}private class wxloginListener implements OnClickListener {@Overridepublic void onClick(View arg0) { wx = ShareSDK.getPlatform(Wechat.NAME); System.out.println('微信是否已經(jīng)授權(quán)1:'+wx.isAuthValid()); if(wx.isAuthValid()){wx.removeAccount(true); } System.out.println('微信是否已經(jīng)授權(quán)2:'+wx.isAuthValid()); wx.SSOSetting(false); // 設(shè)置false表示使用SSO授權(quán)方式 wx.authorize(); wx.showUser(null); wx.setPlatformActionListener(new PlatformActionListener() {@Overridepublic void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) { System.out.println('wx登錄測試hashMap'+hashMap); System.out.println('wx登錄測試hashMap'+hashMap.toString()); String unionid = (String) hashMap.get('unionid'); System.out.println('wx登錄測試unionid'+unionid); System.out.println('wx登錄測試'+platform.getDb().toString()); String thirdLoginId = platform.getDb().getUserId(); System.out.println('wx登錄測試thirdLoginId:'+thirdLoginId); String userName = platform.getDb().getUserName(); System.out.println('wx登錄測試userName:'+ userName); String image = platform.getDb().getUserIcon(); System.out.println('wx登錄測試image:'+image); String sex = qq.getDb().getUserGender(); System.out.println('wx登錄測試sex:'+sex); System.out.println('wx登錄測試:'+'成功了');}@Overridepublic void onError(Platform platform, int i, Throwable throwable) {}@Overridepublic void onCancel(Platform platform, int i) {} }); wx.removeAccount(true); System.out.println('微信是否已經(jīng)授權(quán)3:'+wx.isAuthValid());} }}
這是官方提供的接口,
plat. authorize():要功能不要數(shù)據(jù) 單獨授權(quán)(只獲取授權(quán)信息,不獲取用戶信息)plat. showUser(null):要數(shù)據(jù)無需功能à授權(quán)并獲取信息(除了授權(quán)信息,其他用戶信息可以在oncomplete中的hashmap中獲?。?/p>
這樣就拿到的微信的授權(quán),并且獲得數(shù)據(jù),然后將數(shù)據(jù)向數(shù)據(jù)庫中存儲,就可以做微信登錄了??傮w流程來說還是比較簡單,直接調(diào)用接口就行,就是前期的審核準(zhǔn)備工作比較麻煩。
到此這篇關(guān)于Android實現(xiàn)微信登錄的示例代碼的文章就介紹到這了,更多相關(guān)Android 微信登錄內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: