android QQ登录与分享

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013144287/article/details/79887607
android QQ登录与分享

      android QQ登录与分享是我们一个很常见的功能,除了微信登录、QQ登录、还有微博登录,外加上分享,今天就分享一下android的QQ分享与登录,我这里H5开发的和webview配合使用场景,不过就算是原生的,思路也差不多。

一、腾讯开放平台注册应用获取appId,至于怎么注册很简单,这里就不叙述了。

二、下载sdk,下载链接是http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD,下载最新版本,目前好像已经到3.3.1了

三、将下载下来的包里面的open_sdk_r5990_lite.jar包放入工程的libs目录下,如图


三、build.gradle里面加入

// qq登录
implementation files('libs/open_sdk_r5990_lite.jar')

四、AndroidMainfest.xml里面加入权限和activity

1、权限

<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

2、加入activity

<!--qq登录-->
        <activity android:name="com.tencent.tauth.AuthActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!-- 这地方需要用你在开放平台申请的appid:tencent+appid -->
                <data android:scheme="tencent1106320813" />
            </intent-filter>
        </activity>

        <activity android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="behind"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

五、我这里是提供接口给H5端调用,写在了一个JavascriptUtils工具类里面

//QQ登录
private static final String APP_ID = "1105602574";//官方获取的APPID
private Tencent mTencent;
public static BaseUiListener mIUiListener;
public static final int QQ_LOGIN = 6008;
/**
 * QQ登录
 */
@JavascriptInterface
public void qqLogin() {
    //传入参数APPID和全局Context上下文
    if (mTencent == null) {
        mTencent = Tencent.createInstance(APP_ID,mContext);
    }
    mIUiListener = new BaseUiListener(mTencent, mActivity, mContext);
    //all表示获取所有权限
    mTencent.login(mActivity,"all", mIUiListener);
}

/**
 * 分享到QQ好友
 *
 */
@JavascriptInterface
public void qqShareFriends() {
    if (mTencent == null) {
        mTencent = Tencent.createInstance(APP_ID,mContext);
    }
    Bundle params = new Bundle();
    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
    params.putString(QQShare.SHARE_TO_QQ_TITLE, "标题");
    params.putString(QQShare.SHARE_TO_QQ_SUMMARY,"摘要");
    params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://blog.csdn.net/u013451048");
    params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://avatar.csdn.net/C/3/D/1_u013451048.jpg");
    params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "CSDN");
    mTencent.shareToQQ(mActivity, params, new ShareListener(mActivity));
}

/**
 * 分享到QQ空间
 *
 */
@JavascriptInterface
public void qqShareQzone() {
    Bundle params = new Bundle();
    if (mTencent == null) {
        mTencent = Tencent.createInstance(APP_ID,mContext);
    }
    params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
    params.putString(QQShare.SHARE_TO_QQ_TITLE, "标题");
    params.putString(QQShare.SHARE_TO_QQ_SUMMARY,"摘要");
    params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"https://mp.csdn.net/postedit/79887607");
    params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://avatar.csdn.net/C/3/D/1_u013451048.jpg");
    params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "CSDN");
    mTencent.shareToQQ(mActivity, params, new ShareListener(mActivity));
}

六、在唤起QQ登录之后,需要一个回调类BaseUiListener来返回用户信息和检测用户是否登录成功

package com.iwiteks.PalmarTourism.util;

import android.app.Activity;
import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * QQ登录监听类
 * Created by zhaoyi on 2018/3/20.
 */

public class BaseUiListener implements IUiListener {
    private static final String TAG = "BaseUiListener";
    private Tencent mTencent;
    private UserInfo mUserInfo;
    private Context mContext;
    private Activity mActivity;

    public BaseUiListener (Tencent mTencent, Activity mActivity, Context mContext) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.mTencent = mTencent;
    }
    @Override
    public void onComplete(Object response) {
        Toast.makeText(mActivity, "授权成功", Toast.LENGTH_SHORT).show();
        JSONObject obj = (JSONObject) response;
        try {
            String openID = obj.getString("openid");
            String accessToken = obj.getString("access_token");
            String expires = obj.getString("expires_in");
            mTencent.setOpenId(openID);
            mTencent.setAccessToken(accessToken,expires);
            QQToken qqToken = mTencent.getQQToken();
            mUserInfo = new UserInfo(mContext,qqToken);
            mUserInfo.getUserInfo(new IUiListener() {
                @Override
                public void onComplete(Object response) {
                    Log.e(TAG,"登录成功"+ response.toString());
                    Message message = new Message();
                    message.what = JavaScriptUtils.QQ_LOGIN;
                    Gson gson = new Gson();
                    HashMap<String,String> resultMap =  gson.fromJson(response.toString(), HashMap.class);
                    resultMap.put("openid",openID);
                    message.obj = resultMap;
                    JavaScriptUtils.mHandler.sendMessage(message);
                }

                @Override
                public void onError(UiError uiError) {
                    Log.e(TAG,"登录失败"+ uiError.toString());
                }

                @Override
                public void onCancel() {
                    Log.e(TAG,"登录取消");
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(UiError uiError) {
        Toast.makeText(mActivity, "授权失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(mActivity, "授权取消", Toast.LENGTH_SHORT).show();
    }

}

上面用的到handle会传递到JavascriptUtils工具类里面进行处理,如下

// 消息接受handle,android调用js
public static Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        String method = "";
        if (msg.what == QQ_LOGIN) {//语音合成播放完成
            HashMap<String,String> result = (HashMap<String, String>) msg.obj;
            Gson gson = new Gson();
            method = "javascript:_Native_QqLogin('"+gson.toJson(result)+"')";
        }
        mWebView.evaluateJavascript(method, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                //此处为 js 返回的结果
            }
        });
    }
};

这里是android调用js方法传递数据给H5端了

七、在MainActivity的重写方法onActivityResult()里面加入如下代码,不然回调不了是否登录成功,取消等操作

/**
 * QQ分享监听
 */
if (requestCode == 10103) {
    ShareListener myListener = new ShareListener(MainActivity.this);
    Tencent.onActivityResultData(requestCode,resultCode,data,myListener);
}

/**
 * QQ登录监听
 */
if(requestCode == Constants.REQUEST_LOGIN){
    Tencent.onActivityResultData(requestCode,resultCode,data,JavaScriptUtils.mIUiListener);
}
到此,QQ登录与分享集成完成,我的QQ分享参数是死的,可以动态换成参数进行动态传递



阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页