进入QQ互联官网,点击应用开放平台下的管理中心跳转到个人开发者的注册申请界面
2.申请好个人开发者账后,点击创建一个项目,可以获得一个APPID和APPKEY,记得保存,后面要用
3.进入SDK下载页面下载Android_SDK_Vx.x.x,解压之后得到以下
进入libs下将open_sdk_3.5.1.157_re503bfe_lite.jar文件拷贝到项目的libs文件夹下
添加权限
<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" />
添加activity,对应跳转qq登陆的界面
-
<activity
android:name="com.tencent.tauth.AuthActivity"
android:noHistory="true"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tencent1111333510" />
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" />
实现Activity代码:
定义常量
private Tencent mTencent; //Tencent实例
private String APP_ID = "1111333510";
private IUiListener loginListener; //监听
private String SCOPE = "all"; //范围
初始化监听器
private void initQqLogin(){
mTencent = Tencent.createInstance(APP_ID, this);
//创建QQ登录回调接口
loginListener = new IUiListener() {
@Override
public void onComplete(Object o) {
JSONObject jo = (JSONObject) o;
Log.e("COMPLETE:", jo.toString());
//登录成功后回调该方法
Toast.makeText(MainActivity6.this, "登录成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
//登录失败后回调该方法
Toast.makeText(MainActivity6.this, "登录失败", Toast.LENGTH_SHORT).show();
Log.e("LoginError:", uiError.toString());
}
@Override
public void onCancel() {
//取消登录后回调该方法
Toast.makeText(MainActivity6.this, "取消登录", Toast.LENGTH_SHORT).show();
}
@Override
public void onWarning(int i) {
}
};
}
实现登陆结果回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mTencent.onActivityResultData(requestCode, resultCode, data, loginListener);
if (requestCode == Constants.REQUEST_API) {
if (resultCode == Constants.REQUEST_QQ_SHARE ||
resultCode == Constants.REQUEST_QZONE_SHARE ||
resultCode == Constants.REQUEST_OLD_SHARE) {
mTencent.handleResultData(data, loginListener);
}
}
}
在onCreate方法中定义一个按钮初始化环境并调用登录接口:
Button btn = findViewById(R.id.ic_qq);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initQqLogin();
mTencent.login(MainActivity6.this, SCOPE, loginListener);
}
});
效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020122118280067.gif#pic_center
实现Activity代码:
定义常量
private IUiListener shareListener ;
初始化监听器
//QQ分享监听
private void initShare(){
mTencent = Tencent.createInstance(APP_ID, this);
shareListener = new IUiListener() {
@Override
public void onComplete(Object o) {
//分享成功后回调
Toast.makeText(MainActivity6.this, "分享成功!", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
//分享失败后回调
}
@Override
public void onCancel() {
//取消分享后回调
}
@Override
public void onWarning(int i) {
}
};
}
实现分享方法,更多类型的分享可以参考QQ分享
public void share(View view)
{
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(MainActivity6.this, params, shareListener);
}
在onCreate方法中定义一个按钮初始化环境并调用分享:
initShare();
share(v);
分享效果:
分享失败原因是appid未通过审核,只允许开发者进行第三方登陆
2020-12-21 15:46:44.906 5812-5861/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: com.example.myapplication, PID: 5812
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/conn/scheme/SchemeRegistry;
at com.tencent.open.utils.HttpUtils.getHttpClient(ProGuard:634)
at com.tencent.open.utils.HttpUtils.openUrl2(ProGuard:507)
at com.tencent.open.utils.g$1.run(ProGuard:197)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.conn.scheme.SchemeRegistry" on path: DexPathList[[zip file "/data/app/~~5B-Ts-GeXWFIk_rTJLbHvA==/com.example.myapplication-K1q4IBrjVFa7bt0J17Ckxg==/base.apk"],nativeLibraryDirectories=[/data/app/~~5B-Ts-GeXWFIk_rTJLbHvA==/com.example.myapplication-K1q4IBrjVFa7bt0J17Ckxg==/lib/x86, /system/lib, /system_ext/lib, /product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.tencent.open.utils.HttpUtils.getHttpClient(ProGuard:634)
at com.tencent.open.utils.HttpUtils.openUrl2(ProGuard:507)
at com.tencent.open.utils.g$1.run(ProGuard:197)
原因: 从 Android 9 开始,默认情况下Apache HTTP 客户端内容库已从 bootclasspath 中移除且不可用于应用。
解决:
在AndroidManifest.xml的application中设置此属性
android:usesCleartextTraffic="true"
且在application节点下添加以下内容
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
参考文章:
QQ官方文档:https://wiki.connect.qq.com/qq%E7%99%BB%E5%BD%95
CSDN文章:https://blog.csdn.net/chinaboyliusir/article/details/82755193
作者:林振楠
原文链接