android客户端 接入QQ登录demo

效果图:

 

1、需要到http://open.qq.com/ 注册你的开发者账号,得到APPID

2、Mainfest如配置

权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在<application>标签下

<activity 
android:name="com.tencent.tauth.AuthActivity" 
android:noHistory="true" 
android:launchMode="singleTask" >
<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="你的APPID:1107733709" />
</intent-filter>
</activity>
<activity 
android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize" />

  

3、MainActivity中创建Tencent实例

如下全部代码:

package com.example.mylogin;

import org.json.JSONException;
import org.json.JSONObject;

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 android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
	private Button btn_login;
	private Tencent mTencent;
	private String APP_ID="1107733709";
	private BaseUiListener mUiListener;
	private UserInfo muserInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//创建mTencent实例
		mTencent=Tencent.createInstance(APP_ID, this.getApplicationContext());
		btn_login=(Button) findViewById(R.id.btn_login); 
		/**
		 * qq登录入口
		 */
		btn_login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mUiListener=new BaseUiListener();
				mTencent.login(MainActivity.this, "all", mUiListener);
			}
		});
	}
	private class BaseUiListener implements IUiListener{
		
		//完成
		@Override
		public void onComplete(Object response) {
			Toast.makeText(getApplication(), "授权成功", Toast.LENGTH_LONG).show();
			JSONObject json=(JSONObject)response;
			try {
				String openID=json.getString("openid");
				String accessToken=json.getString("access_token");
				String expires=json.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 onError(UiError arg0) {
						Log.d("onError", "失败");
					}
					
					//成功
					@Override
					public void onComplete(Object arg0) {
						Log.d("onComplete", "成功");
					}
					//取消
					@Override
					public void onCancel() {
						Log.d("onCancel", "取消");
					}
				});
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		//取消
		@Override
		public void onCancel() {
			Toast.makeText(getApplication(), "授权取消", Toast.LENGTH_LONG).show();
		}

		//错误
		@Override
		public void onError(UiError arg0) {
			Toast.makeText(getApplication(), "授权失败", Toast.LENGTH_LONG).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,mUiListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
	}
}

原文地址:https://blog.csdn.net/sandyran/article/details/53319846

 

posted on 2018-08-30 16:07  新禧  阅读(492)  评论(0编辑  收藏  举报

导航