Android实现微信登录的示例代码
作者:hequnwang10 时间:2022-04-04 15:55:56
微信登录的实现与qq登录类似。不过微信登录比较麻烦,需要拿到开发者资质认证,花300块钱,然后应用的话还得有官网之类的,就是比较繁琐的前期准备工作,如果在公司里,这些应该都不是事,会有相关人提前准备好。在这里我们已经拿到了开发者认证,并且申请到了微信登录的授权。
现在直接介绍mob来实现微信登录的代码,并获取微信的相关数据,比较简单。
一、布局界面
布局界面只需要一个button来触发授权就可以
<Button
android:id="@+id/wxlogin"
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 {
@Override
public void onClick(View arg0) {
wx = ShareSDK.getPlatform(Wechat.NAME);
System.out.println("微信是否已经授权1:"+wx.isAuthValid());
if(wx.isAuthValid()){
wx.removeAccount(true);
}
System.out.println("微信是否已经授权2:"+wx.isAuthValid());
wx.SSOSetting(false); // 设置false表示使用SSO授权方式
wx.authorize();
wx.showUser(null);
wx.setPlatformActionListener(new PlatformActionListener() {
@Override
public 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登录测试:"+"成功了");
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
}
@Override
public void onCancel(Platform platform, int i) {
}
});
wx.removeAccount(true);
System.out.println("微信是否已经授权3:"+wx.isAuthValid());
}
}
}
这是官方提供的接口,
plat. authorize():要功能不要数据 单独授权(只获取授权信息,不获取用户信息)plat. showUser(null):要数据无需功能à授权并获取信息(除了授权信息,其他用户信息可以在oncomplete中的hashmap中获取)
这样就拿到的微信的授权,并且获得数据,然后将数据向数据库中存储,就可以做微信登录了。
总体流程来说还是比较简单,直接调用接口就行,就是前期的审核准备工作比较麻烦。
来源:https://blog.csdn.net/hequnwang10/article/details/119464076
标签:Android,微信,登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决IDEA和CMD中java命令提示错误: 找不到或无法加载主类的问题
2023-09-19 02:31:16
![](https://img.aspxhome.com/file/2023/2/101922_0s.png)
c# JSON返回格式的WEB SERVICE
2022-04-16 01:43:31
DevExpress实现禁用TreeListNode CheckBox的方法
2022-07-20 22:17:43
![](https://img.aspxhome.com/file/2023/3/85663_0s.png)
JavaWeb实现注册用户名检测
2022-07-20 05:25:46
![](https://img.aspxhome.com/file/2023/7/92687_0s.jpg)
Spring MVC数据绑定方式
2023-03-18 03:06:55
Springboot整合knife4j与shiro的操作
2023-08-25 08:31:42
![](https://img.aspxhome.com/file/2023/0/76280_0s.png)
SpringBoot拦截器的配置使用介绍
2021-06-20 07:25:54
![](https://img.aspxhome.com/file/2023/5/92655_0s.png)
java项目实现统一打印入参出参等日志
2023-05-25 18:11:06
C#特性 匿名类型与隐式类型局部变量使用介绍
2023-09-29 12:42:50
浅谈C# StringBuilder内存碎片对性能的影响
2023-12-15 08:55:47
浅谈@Aspect@Order各个通知的执行顺序
2021-10-24 19:00:38
![](https://img.aspxhome.com/file/2023/3/94313_0s.png)
c# 使用模式匹配以及 is 和 as 运算符安全地进行强制转换
2022-11-23 09:47:51
java 使用JDBC构建简单的数据访问层实例详解
2023-01-13 05:12:24
使用Java操作TensorFlow的方法
2023-11-27 21:31:38
Java如何实现登录token令牌
2022-05-19 16:58:11
![](https://img.aspxhome.com/file/2023/4/59934_0s.jpg)
Java Swing JList列表框的实现
2021-09-05 08:20:56
![](https://img.aspxhome.com/file/2023/4/80894_0s.gif)
Spring Security基于JWT实现SSO单点登录详解
2022-02-28 10:56:55
![](https://img.aspxhome.com/file/2023/5/71585_0s.jpg)
jvm crash的崩溃日志详细分析及注意点
2022-12-18 07:49:00
c#实现16进制和字符串之间转换的代码
2023-07-22 08:28:09
公共POI导出Excel方法详解
2023-09-10 22:51:31
![](https://img.aspxhome.com/file/2023/3/94213_0s.png)