Android實(shí)現(xiàn)QQ登錄功能
QQ登錄是一個(gè)非常簡(jiǎn)單的一個(gè)第三方應(yīng)用,現(xiàn)在,我們就來(lái)實(shí)現(xiàn)一個(gè)QQ登錄
首先下載兩個(gè)jar包 這里上傳不了jar包,所以可以到我的github中下載工程中l(wèi)ibs中的兩個(gè)jar包
網(wǎng)址:https://github.com/chengzexiang/qqlogin
打代碼前,先把這些東西寫(xiě)上:
private static final String TAG = "MainActivity"; private static final String APP_ID = "1105602574";//官方獲取的APPID private Tencent mTencent; private BaseUiListener mIUiListener; private UserInfo mUserInfo;
在AndroidManifest.xml中加入權(quán)限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 注冊(cè)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" /> <!-- 開(kāi)放平臺(tái)獲取的APPID -->
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait"/>
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" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.czx.czx0914qq.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登錄" android:id="@+id/login"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/name"/> <ImageView android:layout_width="100dp" android:layout_height="80dp" android:id="@+id/img"/> </LinearLayout>
下面開(kāi)始MainActivity中的代碼
package com.bwei.czx.czx0914qq;
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.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
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 TAG = "MainActivity";
private static final String APP_ID = "1105602574";//官方獲取的APPID
private Tencent mTencent;
private BaseUiListener mIUiListener;
private UserInfo mUserInfo;
private Button login;
private TextView name;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//傳入?yún)?shù)APPID和全局Context上下文
mTencent = Tencent.createInstance(APP_ID, MainActivity.this.getApplicationContext());
initView();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIUiListener = new BaseUiListener();
//all表示獲取所有權(quán)限
mTencent.login(MainActivity.this,"all", mIUiListener);
mUserInfo = new UserInfo(MainActivity.this, mTencent.getQQToken()); //獲取用戶信息
mUserInfo.getUserInfo(mIUiListener);
}
});
}
private void initView() {
login = (Button) findViewById(R.id.login);
name = (TextView) findViewById(R.id.name);
img = (ImageView) findViewById(R.id.img);
}
/**
* 自定義監(jiān)聽(tīng)器實(shí)現(xiàn)IUiListener接口后,需要實(shí)現(xiàn)的3個(gè)方法
* onComplete完成 onError錯(cuò)誤 onCancel取消
*/
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
Toast.makeText(MainActivity.this, "授權(quán)成功", 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(Object response) {
Log.e(TAG,"登錄成功"+response.toString());
if(response == null){
return;
}
try {
JSONObject jo = (JSONObject) response;
Toast.makeText(MainActivity.this, "登錄成功",
Toast.LENGTH_LONG).show();
String nickName = jo.getString("nickname");
String figureurl_1= jo.getString("figureurl_1");
name.setText(nickName);
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(MainActivity.this).build();
ImageLoader.getInstance().init(configuration);
ImageLoader.getInstance().displayImage(figureurl_1,img);
} catch (Exception e) {
// TODO: handle exception
}
}
@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, "授權(quán)失敗", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "授權(quán)取消", Toast.LENGTH_SHORT).show();
}
}
@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);
}
}
下面為顯示效果

qq登錄完成!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)單的QQ登錄界面的示例代碼
- Android使用友盟集成QQ、微信、微博等第三方分享與登錄方法詳解
- Android仿QQ在狀態(tài)欄顯示登錄狀態(tài)效果
- Android Studio實(shí)現(xiàn)第三方QQ登錄操作代碼
- Android第三方登錄之QQ登錄
- Android第三方登錄之騰訊QQ登錄的實(shí)例代碼
- Android QQ登錄界面繪制代碼
- Android實(shí)現(xiàn)QQ登錄界面遇到問(wèn)題及解決方法
- Android調(diào)用第三方QQ登錄代碼分享
- Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面
相關(guān)文章
Android開(kāi)發(fā)自定義TextView省略號(hào)樣式的方法
這篇文章主要介紹了Android開(kāi)發(fā)自定義TextView省略號(hào)樣式的方法,結(jié)合實(shí)例形式分析了Android文本控件TextView相關(guān)屬性與字符串操作技巧,需要的朋友可以參考下2017-10-10
Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來(lái)和大家詳細(xì)聊聊2023-01-01
Android實(shí)現(xiàn)底部導(dǎo)航欄功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部導(dǎo)航欄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)
這篇文章主要為大家介紹了Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android組件Glide實(shí)現(xiàn)圖片平滑滾動(dòng)效果
這篇文章主要介紹了Android組件Glide實(shí)現(xiàn)圖片平滑滾動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,需要的朋友可以參考下2016-07-07
Android筆記之:深入ViewStub的應(yīng)用
本篇文章是對(duì)Android中ViewStub的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
這篇文章主要為大家詳細(xì)介紹了Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
實(shí)時(shí)獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享
本文我們分享一個(gè)實(shí)時(shí)獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享,可以作為學(xué)習(xí)使用,本文貼出部分重要代碼,需要的朋友可以參考下本文2015-09-09

