Android Studio實(shí)現(xiàn)第三方QQ登錄操作代碼
來(lái)看看效果圖吧


http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD 下載SDKJar包 接下來(lái)就可以
實(shí)現(xiàn)QQ登錄了,
新建一個(gè)項(xiàng)目工程 ,然后把我們剛才下載的SDK解壓將jar文件夾中的jar包拷貝到我們的項(xiàng)目libs中

導(dǎo)入一個(gè)下面架包就可以

項(xiàng)目結(jié)構(gòu)如下

打開我們的清單文件Androidmanifest 在里面加入權(quán)限和注冊(cè)Activity 如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tangxiaoying.qq2">
<!-- QQ登錄授權(quán)所需權(quán)限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<!-- 注冊(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" /> <!-- 開放平臺(tái)獲取的APPID -->
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait"/>
</application>
</manifest>
布局文件activity_main 就一個(gè)Button按鈕
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點(diǎn)擊QQ登錄" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/> </RelativeLayout>
下面就是我們的MainActivity中的代碼了
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.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;
@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());
}
public void buttonLogin(View v){
/**通過(guò)這句代碼,SDK實(shí)現(xiàn)了QQ的登錄,這個(gè)方法有三個(gè)參數(shù),第一個(gè)參數(shù)是context上下文,第二個(gè)參數(shù)SCOPO 是一個(gè)String類型的字符串,表示一些權(quán)限
官方文檔中的說(shuō)明:應(yīng)用需要獲得哪些API的權(quán)限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有權(quán)限用“all”
第三個(gè)參數(shù),是一個(gè)事件監(jiān)聽器,IUiListener接口的實(shí)例,這里用的是該接口的實(shí)現(xiàn)類 */
mIUiListener = new BaseUiListener();
//all表示獲取所有權(quán)限
mTencent.login(MainActivity.this,"all", mIUiListener);
}
/**
* 自定義監(jiān)聽器實(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());
}
@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();
}
}
/**
* 在調(diào)用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);
}
}
總結(jié)
以上所述是小編給大家介紹的Android Studio實(shí)現(xiàn)第三方QQ登錄操作代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android Studio實(shí)現(xiàn)簡(jiǎn)單的QQ登錄界面的示例代碼
- Android使用友盟集成QQ、微信、微博等第三方分享與登錄方法詳解
- Android仿QQ在狀態(tài)欄顯示登錄狀態(tài)效果
- Android第三方登錄之QQ登錄
- Android實(shí)現(xiàn)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)文章
Flutter實(shí)現(xiàn)可循環(huán)輪播圖效果
這篇文章主要介紹了Flutter實(shí)現(xiàn)可循環(huán)輪播圖效果,本文圖文并茂通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07
Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化
這篇文章主要介紹了Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化的相關(guān)資料,Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android開發(fā)常用標(biāo)簽小結(jié)
這篇文章主要介紹了Android開發(fā)常用標(biāo)簽,分析總結(jié)了Android開發(fā)中常見標(biāo)簽的使用技巧,需要的朋友可以參考下2015-05-05
Android View實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android中判斷網(wǎng)絡(luò)連接狀態(tài)的方法
App判斷用戶是否聯(lián)網(wǎng)是很普遍的需求,這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)連接狀態(tài)的方法,感興趣的朋友可以參考一下2016-02-02

