1. 导包添加依赖:
(1)gson包解析数据;
(2)glide包加载图片;
(3)open_sdk_r5886_lite.jar包 第三方key。
2. 登录页面:
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; import com.google.gson.Gson; import com.tencent.connect.UserInfo; import com.tencent.connect.auth.QQToken; import com.tencent.connect.common.Constants; import com.tencent.tauth.IUiListener; import com.tencent.tauth.Tencent; import com.tencent.tauth.UiError; import org.json.JSONException; import org.json.JSONObject; //登录页面 public class MainActivity extends AppCompatActivity { private static final String APP_ID = "1105602574";//官方获取的APPID private static final String TAG = "MainActivity"; private BaseUiListener mIUiListener; 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()); } public void buttonLogin(View v){ /**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限 官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all” 第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */ mIUiListener = new BaseUiListener(); //all表示获取所有权限 mTencent.login(MainActivity.this,"all", mIUiListener); } /** * 自定义监听器实现IUiListener接口后,需要实现的3个方法 * onComplete完成 onError错误 onCancel取消 */ private class BaseUiListener implements IUiListener { @Override public void onComplete(Object response) { Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show(); Log.e(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(final Object response) { //用户登录成功后会返回用户的数据,在此可以封装成bean类,跳转传值,设置用户的基本信息 Log.i(TAG,"登录成功"+response.toString()); runOnUiThread(new Runnable() { @Override public void run() { //跳转传值,设置用户信息 UserBean userBean = new Gson().fromJson(response.toString(), UserBean.class); Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("headPhoto",userBean.getFigureurl_qq_1()); intent.putExtra("name",userBean.getNickname()); intent.putExtra("gender",userBean.getGender()); startActivity(intent); } }); } @Override public void onError(UiError uiError) { Log.e(TAG,"登录失败"+uiError.toString()); } @Override public void onCancel() { Log.e(TAG,"登录取消"); } }); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(UiError uiError) { Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show(); } } /** * 在调用Login的Activity或者Fragment中重写onActivityResult方法 * @param requestCode * @param resultCode * @param data */ @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); } }
3.用户登录成功后会返回用户的数据,可以封装成bean类(此类根据自己需求判断是否进行添加)
/** * 用户登录成功后返回的数据 */ public class UserBean { private String is_yellow_year_vip; private int ret; private String figureurl_qq_1; private String figureurl_qq_2; private String nickname; private String yellow_vip_level; private int is_lost; private String msg; private String city; private String figureurl_1; private String vip; private String level; private String figureurl_2; private String province; private String is_yellow_vip; private String gender; private String figureurl; public String getIs_yellow_year_vip() { return is_yellow_year_vip; } public void setIs_yellow_year_vip(String is_yellow_year_vip) { this.is_yellow_year_vip = is_yellow_year_vip; } public int getRet() { return ret; } public void setRet(int ret) { this.ret = ret; } public String getFigureurl_qq_1() { return figureurl_qq_1; } public void setFigureurl_qq_1(String figureurl_qq_1) { this.figureurl_qq_1 = figureurl_qq_1; } public String getFigureurl_qq_2() { return figureurl_qq_2; } public void setFigureurl_qq_2(String figureurl_qq_2) { this.figureurl_qq_2 = figureurl_qq_2; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getYellow_vip_level() { return yellow_vip_level; } public void setYellow_vip_level(String yellow_vip_level) { this.yellow_vip_level = yellow_vip_level; } public int getIs_lost() { return is_lost; } public void setIs_lost(int is_lost) { this.is_lost = is_lost; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getFigureurl_1() { return figureurl_1; } public void setFigureurl_1(String figureurl_1) { this.figureurl_1 = figureurl_1; } public String getVip() { return vip; } public void setVip(String vip) { this.vip = vip; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getFigureurl_2() { return figureurl_2; } public void setFigureurl_2(String figureurl_2) { this.figureurl_2 = figureurl_2; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getIs_yellow_vip() { return is_yellow_vip; } public void setIs_yellow_vip(String is_yellow_vip) { this.is_yellow_vip = is_yellow_vip; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getFigureurl() { return figureurl; } public void setFigureurl(String figureurl) { this.figureurl = figureurl; } }
4. 设置用户信息的页面
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; //显示用户数据的页面 public class SecondActivity extends AppCompatActivity{ private ImageView headPhoto; private TextView userName; private TextView userSex; private Button outLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); //初始化数据 //设置用户信息 Intent intent=getIntent(); String photo = intent.getStringExtra("headPhoto"); String name= intent.getStringExtra("name"); String sex= intent.getStringExtra("gender"); Glide.with(this).load(photo).into(headPhoto); userName.setText(" "+name); userSex.setText(" "+sex); } private void initView() { headPhoto = (ImageView) findViewById(R.id.headPhoto); userName = (TextView) findViewById(R.id.name); userSex = (TextView) findViewById(R.id.sex); outLogin = (Button) findViewById(R.id.outLogin); outLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(SecondActivity.this,MainActivity.class); startActivity(intent); finish(); } }); } }
5. 登录页面布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.rikao09.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击QQ登录" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/> </RelativeLayout>
6.用户信息页面布局: activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="36dp" android:text="用户信息" /> <ImageView android:src="@drawable/qq" android:id="@+id/headPhoto" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="36dp" android:layout_gravity="center_horizontal"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别:" android:textSize="20dp" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="108dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄:" android:textSize="20dp" /> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> </LinearLayout> <Button android:text="退出登录" android:id="@+id/outLogin" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
7. 最后设置清单文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bwie.thirdlogin_qq"> <!-- QQ登录授权所需权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@drawable/qq" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 注册SDKActivity --> <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="tencent1105602574" /> <!-- 开放平台获取的APPID --> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <activity android:name=".SecondActivity"></activity> </application> </manifest>