一般的项目中都会用到第三方登录,常见的有qq,微信,微博,这里给大家实现qq第三方登录的方法。第三方登陆的方法有很多,有集成友盟的,有集成mob的,还有使用腾讯官方sdk的,个人认为官方的最方便一点,当然别人集成的也有好处,毕竟别人也写了好多代码,但就是怕他们写的那些代码有冗余。贴代码:
先要去腾讯官方open.qq官方网址下载sdk:http://wiki.open.qq/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD我的是现在了最新最上变的那个,然后解压把jar文件夹的那个jar文件直接拷到libs文件夹下。
1,配置AndroidManifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
</intent-filter>
</activity>
<!-- qqSDK_V2.0引入了AssistActivity -->
<activity
android:name="com.tencent.connectmon.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
fragment_login.xml:
<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
<include
android:id="@+id/login_title"
layout="@layout/include_title" />
<LinearLayout
android:id="@+id/login_linear_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<EditText
android:id="@+id/login_user_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_launcher"
android:hint="请输入用户名:" />
<EditText
android:id="@+id/login_pwd_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_launcher"
android:hint="请输入密码:" />
</LinearLayout>
<TextView
android:id="@+id/login_forget_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/login_linear_user"
android:text="忘记密码" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/login_forget_pwd"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="登录" />
<Button
android:id="@+id/login_register_btn"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="注册" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/login_third_party"
android:layout_centerHorizontal="true"
android:text="—————第三方快速登录——————" />
<LinearLayout
android:id="@+id/login_third_party"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/login_qq"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="6dp"
android:background="@drawable/third_party_qq" />
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/third_party_wx" />
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="6dp"
android:background="@drawable/third_party_xl" />
</LinearLayout>
</RelativeLayout>
LoginFragment.java:
package com.example.demo.fragment;
import org.json.JSONObject;
import com.example.sihaiproject.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.tencent.connect.UserInfo;
import com.tencent.connectmon.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginFragment extends Fragment {
private View view;
@ViewInject(R.id.login_qq)
private ImageButton loginQq;
private Tencent mTencent;
private String APP_ID = "1105225312";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = View.inflate(getActivity(), R.layout.fragment_login, null);
ViewUtils.inject(this, view);
return view;
}
@OnClick(R.id.login_qq)
public void loginQq(View view) {
mTencent = Tencent.createInstance(APP_ID, getActivity());
if (!mTencent.isSessionValid())
{
mTencent.login(this, "all", loginListener);
getUserInfo();
}
}
IUiListener loginListener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
System.out.println("loginListener" + values.toString());
/**
* 获取用用户信息前必须调用。
*/
initOpenidAndToken(values);
// 获取用户信息
getUserInfo();
}
};
/**
* 获取用户信息
*/
public void getUserInfo() {
if (mTencent != null && mTencent.isSessionValid()) {
IUiListener listener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
System.out.println("updateUserInfo------" + values);
}
};
/**
* UserInfo:QQ提供的用户信息类
*/
UserInfo userInfo = new UserInfo(getActivity(), mTencent.getQQToken());
userInfo.getUserInfo(listener);
}
}
/***
* 初始化openid
*
* @param jsonObject
*/
public void initOpenidAndToken(JSONObject jsonObject) {
try {
String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId)) {
mTencent.setAccessToken(token, expires);
mTencent.setOpenId(openId);
}
} catch (Exception e) {
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mTencent.onActivityResultData(requestCode, resultCode, data, loginListener);
}
private class BaseUiListener implements IUiListener {
/**
* 取消授权
*/
@Override
public void onCancel() {
}
/**
* 授权成功
*/
@Override
public void onComplete(Object response) {
JSONObject jsonResponse = (JSONObject) response;
Toast.makeText(getActivity(), jsonResponse.toString(), 1).show();
doComplete(jsonResponse);
}
/**
* 授权失败
*/
@Override
public void onError(UiError arg0) {
}
protected void doComplete(JSONObject values) {
}
}
}
其实也是看官方文档做的,最详细的还是去腾讯开发平台去看详细文档,我这里只不过更清楚的看到一个登陆功能。这些代码运行后可以获取到qq号,qq名称,qq头像……..
如果报错:java.lang.SecurityException: ConnectivityService不用查了,是你配置文件里的<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
没写或写错了。
tencent你的AppId要去开发者平台首页→移动应用→创建应用→获取appId。
java代码我用的是xutils2.x和fastjson,如果直接复制代码会报错的,根据自己的需要提取代码。
更多推荐
android qq第三方登录(腾讯sdk)
发布评论