Android微信登录(shareSDK)注意事项

Android微信登录是个坑(shareSDK)

标签(空格分隔): shareSDK 微信登录 Android


第三方登录,只有微信是最麻烦的东西,之所以说麻烦是官方文档写的真的,网上的各种demo虽然能跑,但是却总有一些小问题注意不到。


首先,注册微信的开发者帐号,注意先生成Key文件,打包,然后再去获得微信需要的两个值。检查工具
下载检查工具
微信包的位置,写法要严格
Android微信登录(shareSDK)注意事项_第1张图片

"com.jiuyuhulian.LiveRoom.wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait"
        />

AndroidStudio的gradle虽然强大,但是也很难掌握,Asset文件夹,最好右键new->Folder->AssetFoleder去建立, 然后将ShareSDK的xml文件放进去。

WXEntryActivity 
代码就不贴了,可以去ShareSDK找最新的Demo, 切记不要拿Demo直接导入,有的版本很旧。
/** 微信登录调用 */
public void WeChatLogin(){
        Platform weixinfd = ShareSDK.getPlatform(Wechat.NAME);
        weixinfd.setPlatformActionListener(this);
        if (weixinfd.isValid ()){
            weixinfd.removeAccount();
        }
        Log.d("LOGIN", "weixinfd.showUser");
        weixinfd.showUser(null);
    }
/**
 * 微信登录回调
 * @param platform
 * @param action
 */
@Override
public void onCancel(Platform platform, int action) {
    Message msg = new Message();
    msg.what = MSG_ACTION_CCALLBACK;
    msg.arg1 = 3;
    msg.arg2 = action;
    msg.obj = platform;
    UIHandler.sendMessage(msg, this);
}

@Override
public void onComplete(Platform platform, int action, HashMap res) {
    Message msg = new Message();
    msg.what = MSG_ACTION_CCALLBACK;
    msg.arg1 = 1;
    msg.arg2 = action;
    System.out.println(res);
    //获取资料
    String nickname = platform.getDb().getUserName();//获取用户名字
    String iconUri = platform.getDb().getUserIcon(); //获取用户头像
    platform.getDb().get("openid");
    String openid =platform.getDb().getUserId();
    String token = platform.getDb().getToken();
    String refresh_token111 = platform.getDb().get("refresh_token");
    String toJSLogin = "{" +
            "  \"headimgurl\" : \"" + iconUri + "\","
            + "  \"nickname\" : \"" + nickname + "\","
            + "  \"openid\" :  \""+ openid + "\""
            + "}";
    Log.d("WXCHATLOGIN", toJSLogin);
    msg.obj = toJSLogin;
    UIHandler.sendMessage(msg, this);

}

@Override
public void onError(Platform platform, int action, Throwable t) {
    t.printStackTrace();

    Message msg = new Message();
    msg.what = MSG_ACTION_CCALLBACK;
    msg.arg1 = 2;
    msg.arg2 = action;
    msg.obj = t;
    UIHandler.sendMessage(msg, this);

    // 分享失败的统计
    ShareSDK.logDemoEvent(4, platform);
}

// 回调handleMessage
@Override
public boolean handleMessage(Message msg) {
    switch (msg.arg1) {
        case 1: {
            // 成功
            String str = (String) msg.obj;
            popFragment.getLoginwebView().loadUrl(
                    String.format("javascript:wxLogin(" + str + ")"));
            Toast.makeText(mContext, "成功", Toast.LENGTH_SHORT).show();
        }
        break;
        case 2: {
            // 失败
            Toast.makeText(mContext, "失败", Toast.LENGTH_SHORT).show();

            String expName = msg.obj.getClass().getSimpleName();
            if ("WechatClientNotExistException".equals(expName)
                    || "WechatTimelineNotSupportedException".equals(expName)
                    || "WechatFavoriteNotSupportedException".equals(expName)) {
                Toast.makeText(mContext, "请安装微信客户端", Toast.LENGTH_SHORT).show();
            }
        }
        break;
        case 3: {
            Toast.makeText(mContext, "取消····", Toast.LENGTH_SHORT)
                    .show();
        }
        break;
    }

    return false;
}

你可能感兴趣的:(Android)