先看下效果图吧 java
原理:咱们要使用QQ登陆咱们的应用,不是不用注册,是咱们在后台为用户注册了,可是用户不知道,注册须要惟一标识,上图的那串字母与数字的组合就是咱们要得到的惟一标识:OpenID。 android
其中,AppConstant中是用来放置APPID的,因为考虑到还可能引入其余第三方登陆,为方便管理,故建立此类。Util是根据路径从网上获取图片的处理类。 json
首先在AndroidManifest.xml中进行两个定义: 网络
<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="tencent1104613941" />
</intent-filter>
</activity> ide
而后是两个权限: 布局
public class MainActivity extends Activity implements OnClickListener { TextView openidTextView; TextView nicknameTextView; Button loginButton; ImageView userlogo; private Tencent mTencent; public static QQAuth mQQAuth; public static String mAppid; public static String openidString; public static String nicknameString; public static String TAG="MainActivity"; Bitmap bitmap = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //用来登陆的Button loginButton=(Button)findViewById(R.id.login); loginButton.setOnClickListener(this); //用来显示OpenID的textView openidTextView=(TextView)findViewById(R.id.user_openid); //用来显示昵称的textview nicknameTextView=(TextView)findViewById(R.id.user_nickname); //用来显示头像的Imageview userlogo=(ImageView)findViewById(R.id.user_logo); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.login: LoginQQ(); break; default: break; } } //这里是调用QQ登陆的关键代码 public void LoginQQ() { //这里的APP_ID请换成你应用申请的APP_ID,我这里使用的是DEMO中官方提供的测试APP_ID 222222 mAppid = AppConstant.APP_ID; //第一个参数就是上面所说的申请的APPID,第二个是全局的Context上下文,这句话实现了调用QQ登陆 mTencent = Tencent.createInstance(mAppid,getApplicationContext()); /**经过这句代码,SDK实现了QQ的登陆,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限 官方文档中的说明:应用须要得到哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;全部权限用“all” 第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */ mTencent.login(MainActivity.this,"all", new BaseUiListener()); } /**当自定义的监听器实现IUiListener接口后,必需要实现接口的三个方法, * onComplete onCancel onError *分别表示第三方登陆成功,取消 ,错误。*/ private class BaseUiListener implements IUiListener { public void onCancel() { // TODO Auto-generated method stub } public void onComplete(Object response) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "登陆成功", 0).show(); try { //得到的数据是JSON格式的,得到你想得到的内容 //若是你不知道你能得到什么,看一下下面的LOG Log.e(TAG, "-------------"+response.toString()); openidString = ((JSONObject) response).getString("openid"); openidTextView.setText(openidString); Log.e(TAG, "-------------"+openidString); //access_token= ((JSONObject) response).getString("access_token"); //expires_in = ((JSONObject) response).getString("expires_in"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } /**到此已经得到OpneID以及其余你想得到的内容了 QQ登陆成功了,咱们还想获取一些QQ的基本信息,好比昵称,头像什么的,这个时候怎么办? sdk给咱们提供了一个类UserInfo,这个类中封装了QQ用户的一些信息,我么能够经过这个类拿到这些信息 如何获得这个UserInfo类呢? */ QQToken qqToken = mTencent.getQQToken(); UserInfo info = new UserInfo(getApplicationContext(), qqToken); //这样咱们就拿到这个类了,以后的操做就跟上面的同样了,一样是解析JSON info.getUserInfo(new IUiListener() { public void onComplete(final Object response) { // TODO Auto-generated method stub Log.e(TAG, "---------------111111"); Message msg = new Message(); msg.obj = response; msg.what = 0; mHandler.sendMessage(msg); Log.e(TAG, "-----111---"+response.toString()); /**因为图片须要下载因此这里使用了线程,若是是想得到其余文字信息直接 * 在mHandler里进行操做 * */ new Thread(){ @Override public void run() { // TODO Auto-generated method stub JSONObject json = (JSONObject)response; try { bitmap = Util.getbitmap(json.getString("figureurl_qq_2")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Message msg = new Message(); msg.obj = bitmap; msg.what = 1; mHandler.sendMessage(msg); } }.start(); } public void onCancel() { Log.e(TAG, "--------------111112"); // TODO Auto-generated method stub } public void onError(UiError arg0) { // TODO Auto-generated method stub Log.e(TAG, "-111113"+":"+arg0); } }); } public void onError(UiError arg0) { // TODO Auto-generated method stub } } Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { JSONObject response = (JSONObject) msg.obj; if (response.has("nickname")) { try { nicknameString=response.getString("nickname"); nicknameTextView.setText(nicknameString); Log.e(TAG, "--"+nicknameString); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }else if(msg.what == 1){ Bitmap bitmap = (Bitmap)msg.obj; userlogo.setImageBitmap(bitmap); } } }; }
上图是登陆Q的返回LOG 测试
publicclassAppConstant { publicstaticString APP_ID="222222"; }
public class Util { public static String TAG="UTIL"; public static Bitmap getbitmap(String imageUri) { Log.v(TAG, "getbitmap:" + imageUri); // 显示网络上的图片 Bitmap bitmap = null; try { URL myFileUrl = new URL(imageUri); HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); Log.v(TAG, "image download finished." + imageUri); } catch (IOException e) { e.printStackTrace(); Log.v(TAG, "getbitmap bmp fail---"); return null; } return bitmap; } }