第三方登入QQ完整版,授權登入
阿新 • • 發佈:2019-01-09
在這個網址裡下載 Android_SDK_V3.3.0
http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD
首先將Android_SDK_V3.3.0.lite下面的open_sdk_r5886_lite.jar貼上到專案的libs下面,
再給專案新增
Library dependency,選擇open_sdk_r5886_lite.jar新增依賴
參考網址: http://blog.csdn.net/sandyran/article/details/53319846
清單檔案中註冊
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lianxiday07_disanfang"> <!-- QQ登入授權所需許可權 --> <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: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"/> </application> </manifest>
activity_main的佈局
<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="點選QQ登入" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/> </RelativeLayout>
MainActivity的程式碼
授權成功後跳轉到的個人中心頁面的佈局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); //傳入引數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(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 (Exception 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); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:textSize="23sp"
android:padding="20dp"
android:text="個人資訊"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp" />
<LinearLayout
android:padding="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="23sp"
android:text="頭像"
/>
<ImageView
android:layout_marginLeft="200dp"
android:src="@drawable/_tou"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="80dp" />
</LinearLayout>
<TextView
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp" />
<LinearLayout
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="23sp"
android:text="使用者名稱"
/>
<TextView
android:layout_marginLeft="170dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="23sp"
android:text="username"
/>
</LinearLayout>
<TextView
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp" />
<LinearLayout
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="23sp"
android:text="性別"
/>
<TextView
android:layout_marginLeft="270dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="23sp"
android:text="女"
/>
</LinearLayout>
<TextView
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp" />
<Button
android:textSize="23sp"
android:layout_marginTop="150dp"
android:text="退出登入"
android:layout_width="200dp"
android:layout_height="60dp" />
</LinearLayout>