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


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

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

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

打開我們的清單文件Androidmanifest 在里面加入權(quán)限和注冊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>
<!-- 注冊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: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){
/**通過這句代碼,SDK實(shí)現(xiàn)了QQ的登錄,這個(gè)方法有三個(gè)參數(shù),第一個(gè)參數(shù)是context上下文,第二個(gè)參數(shù)SCOPO 是一個(gè)String類型的字符串,表示一些權(quán)限
官方文檔中的說明:應(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登錄操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹
這篇文章主要介紹了Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07
React Native開發(fā)中自動(dòng)打包腳本的實(shí)例代碼
這篇文章主要介紹了React Native開發(fā)中自動(dòng)打包腳本的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Android中FoldingLayout折疊布局的用法及實(shí)戰(zhàn)全攻略
這篇文章主要介紹了Android中FoldingLayout折疊布局的用法及實(shí)例,通過FoldingLayout我們可以制作出炫酷的菜單折疊效果,文中的例子講解得非常詳細(xì),需要的朋友可以參考下2016-02-02
Android注冊登錄實(shí)時(shí)自動(dòng)獲取短信驗(yàn)證碼
注冊登錄或修改密碼功能常常需要輸入短信驗(yàn)證碼,如何自動(dòng)獲取短信驗(yàn)證碼,這篇文章就為大家介紹了Androidcv注冊登錄自動(dòng)獲取短信驗(yàn)證碼的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08
Android中WebView的使用與后退鍵處理詳細(xì)講解
博主自從開始寫安卓以來,一直飽受WebView的摧殘,好在網(wǎng)上一大堆的大神給出了他們成長路上遇到的坑以及一些解決辦法,這篇文章主要給大家介紹了關(guān)于Android中WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04

