1:首先可以去腾讯第三方的官方平台去下载文档和SDK看一下。http://open.qq.com/
2:步骤演示:
添加jar包,这是在官方平台下载的。
3.添加的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4:在应用的AndroidManifest.xml增加配置的<application>节点下增加以下配置:
<application>
<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="tencent你的AppId" /> <!-- 开放平台获取的APPID --> </intent-filter> </activity>
<activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:screenOrientation="portrait"/><application>
5:代码:
private UserInfo mUserInfo;
private Tencent mTencent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//传入参数APPID和全局Context上下文
mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BaseUiListener mIUiListener = new BaseUiListener();
//all表示获取所有权限
mTencent.login(NewsActivity.this,"all", mIUiListener);
}
});}
//实现官方的接口,重写方法
private class BaseUiListener implements IUiListener{
@Override
public void onComplete(Object response) {
Log.i(TAG, "response:" + response);
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(getApplicationContext(),qqToken);
mUserInfo.getUserInfo(new IUiListener() {
@Override
public void onComplete(Object response) {
Log.i(TAG,"登录成功"+response.toString());
JSONObject res= (JSONObject) response;
String nickname = res.optString("nickname");
String figureurl_qq_1 = res.optString("figureurl_qq_1");
Log.e(TAG,nickname+" "+figureurl_qq_1);
text_nickname.setText(nickname);
x.image().bind(pic_title,figureurl_qq_1,options)
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
}
}
/**
* 回调方式是必须写的,否则执行不成功
*
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == Constants.REQUEST_LOGIN){
Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
}