1.按照文档,下载SDK,将jar放入libs,
2.配置AndroidManifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 从QQ登录中copy的代码-->
<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" />
<data android:scheme="tencent1105546048" />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
3.创建实例:
// Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
// 其中APP_ID是分配给第三方应用的appid,类型为String。
mTencent = Tencent.createInstance(APP_ID, this);
这里实例化的时候一直崩溃,信息:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/conn/scheme/SchemeRegistry;
解决:
在AndroidManifest.xml文件的application标签里面加入
<uses-library android:name="org.apache.http.legacy" android:required="false" />
4.实现回调 IUiListener
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_LOGIN ||
requestCode == Constants.REQUEST_APPBAR) {
Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
IUiListener loginListener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
}
};
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
if (null == response) {
Log.d(TAG, "返回为空, 登录失败");
return;
}
JSONObject jsonResponse = (JSONObject) response;
if (null != jsonResponse && jsonResponse.length() == 0) {
Log.d(TAG, "返回为空, 登录失败");
return;
}
Util.showResultDialog(MainActivity.this, response.toString(), "登录成功");
doComplete((JSONObject) response);
}
protected void doComplete(JSONObject values) {
}
@Override
public void onError(UiError e) {
}
@Override
public void onCancel() {
}
}
5.跳转到QQ请求登录
mTencent.login(this, "all", loginListener);