第一步,要先申请QQ互联接入的APP ID和APP KEY,申请步骤可以到QQ互联官网查看。
第二步,下载QQ互联官方Android SDK
包里面有:(doc是SDK使用说明书,jar是接入的API,sample是事例代码,tools里面有获取签名APK)
第三步,创建Andorid工程,在工程libs目录下导入两个包
第四步,配置manifest.xml文件
(1)添加权限
<!-- 访问网路状态的权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 访问网路的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
(2)添加QQ登录授权登录界面(对于开发者来说只要添加如下配置即可)
<!-- 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" />
<data android:scheme="tencent你申请的API KEY" />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
第五步,创建Tencent类(接入API)的实现类(在该类中实例化Tencent类)
package com.liujun.toword.activity;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import android.app.Activity;
import android.content.Intent;
public abstract class BaseActivity extends Activity {
public static final String AppId="你申请的API KEY";
protected Tencent mTencent;
public static final String SCOPE="get_user_info";
public void initTencent(){
mTencent=Tencent.createInstance(AppId, this.getApplicationContext());
}
public void login(){
mTencent.login(this, SCOPE, new IUiListener() {
@Override
public void onError(UiError arg0) {
showMessage("Login Error:"+arg0.errorMessage);
}
@Override
public void onComplete(Object arg0) {
showMessage("Login Success:"+arg0.toString());
}
@Override
public void onCancel() {
showMessage("Login Cancel:");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mTencent!=null) {
mTencent.onActivityResult(requestCode, resultCode, data);
}
}
public abstract void showMessage(String msg);
}
第六步,操作Tencent类,实现QQ登录第三方Android 应用
package com.liujun.toword.activity;
import com.liujun.toword.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class LoginActivity extends BaseActivity {
private Button loginBtn;
private TextView loginMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginBtn=(Button) this.findViewById(R.id.login);
loginMessage=(TextView) this.findViewById(R.id.message);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initTencent();
login();
}
});
}
@Override
public void showMessage(String msg) {
loginMessage.setText(msg);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="login" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dip" />
</LinearLayout>
/
支持QQ互联登录自己开发的Andorid应用就实现了