Android实现QQ登录功能

 更新时间:2017年09月28日 09:03:50   作者:程泽翔  
这篇文章主要为大家详细介绍了Android实现QQ登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

QQ登录是一个非常简单的一个第三方应用,现在,我们就来实现一个QQ登录
首先下载两个jar包   这里上传不了jar包,所以可以到我的github中下载工程中libs中的两个jar包

网址:https://github.com/chengzexiang/qqlogin

打代码前,先把这些东西写上:

private static final String TAG = "MainActivity"; 
private static final String APP_ID = "1105602574";//官方获取的APPID 
private Tencent mTencent; 
private BaseUiListener mIUiListener; 
private UserInfo mUserInfo; 

在AndroidManifest.xml中加入权限  

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <!-- 注册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"/> 

 Xml布局中的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 android:orientation="vertical" 
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
 android:layout_height="match_parent" tools:context="com.bwei.czx.czx0914qq.MainActivity"> 
 
 <Button 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="登录" 
  android:id="@+id/login"/> 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:id="@+id/name"/> 
 <ImageView 
  android:layout_width="100dp" 
  android:layout_height="80dp" 
  android:id="@+id/img"/> 
 
</LinearLayout> 

下面开始MainActivity中的代码

package com.bwei.czx.czx0914qq; 
 
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.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import com.nostra13.universalimageloader.core.ImageLoader; 
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 
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; 
 private Button login; 
 private TextView name; 
 private ImageView img; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  //传入参数APPID和全局Context上下文 
  mTencent = Tencent.createInstance(APP_ID, MainActivity.this.getApplicationContext()); 
 
  initView(); 
  login.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    mIUiListener = new BaseUiListener(); 
    //all表示获取所有权限 
    mTencent.login(MainActivity.this,"all", mIUiListener); 
    mUserInfo = new UserInfo(MainActivity.this, mTencent.getQQToken()); //获取用户信息 
    mUserInfo.getUserInfo(mIUiListener); 
   } 
  }); 
 } 
 
 private void initView() { 
  login = (Button) findViewById(R.id.login); 
  name = (TextView) findViewById(R.id.name); 
  img = (ImageView) findViewById(R.id.img); 
 } 
 /** 
  * 自定义监听器实现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()); 
      if(response == null){ 
       return; 
      } 
      try { 
       JSONObject jo = (JSONObject) response; 
        Toast.makeText(MainActivity.this, "登录成功", 
          Toast.LENGTH_LONG).show(); 
       String nickName = jo.getString("nickname"); 
       String figureurl_1= jo.getString("figureurl_1"); 
       name.setText(nickName); 
       ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(MainActivity.this).build(); 
       ImageLoader.getInstance().init(configuration); 
       ImageLoader.getInstance().displayImage(figureurl_1,img); 
      } catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 
 
     @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, "授权失败", Toast.LENGTH_SHORT).show(); 
 
  } 
 
  @Override 
  public void onCancel() { 
   Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show(); 
 
  } 
 
 } 
 @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); 
 } 
} 

下面为显示效果

qq登录完成!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android如何实现NFC读取卡片信息

    Android如何实现NFC读取卡片信息

    这篇文章主要介绍了Android如何实现NFC读取卡片信息问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Android 7.0 SEAndroid app权限配置方法

    Android 7.0 SEAndroid app权限配置方法

    今天小编就为大家分享一篇Android 7.0 SEAndroid app权限配置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Android判断App前台运行还是后台运行(运行状态)

    Android判断App前台运行还是后台运行(运行状态)

    这篇文章主要介绍了Android判断App前台运行还是后台运行的相关资料,需要的朋友可以参考下
    2016-04-04
  • Android实现微信分享带有缩略图的网页

    Android实现微信分享带有缩略图的网页

    最近做了一个web app的项目,要求分享web页还要带有图片功能,怎么实现呢?今天小编给大家分享android实现微信分享带有缩略图的网页功能,需要的朋友参考下
    2017-02-02
  • Android APK优化工具Zipalign详解

    Android APK优化工具Zipalign详解

    本文主要介绍Android APK优化工具Zipalign,这里整理了相关资料,并详细介绍如何使用Zipalign工具及使用技巧,有需要的小伙伴可以参考下
    2016-09-09
  • Flutter runApp GestureBinding使用介绍

    Flutter runApp GestureBinding使用介绍

    这篇文章主要为大家介绍了Flutter runApp GestureBinding使用介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • AndroidStudio 配置 AspectJ 环境实现AOP的方法

    AndroidStudio 配置 AspectJ 环境实现AOP的方法

    本篇文章主要介绍了AndroidStudio 配置 AspectJ 环境实现AOP的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android SearchView搜索控件使用方法详解

    Android SearchView搜索控件使用方法详解

    这篇文章主要为大家详细介绍了Android SearchView搜索控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • MPAndroidChart绘制自定义运动数据图表示例详解

    MPAndroidChart绘制自定义运动数据图表示例详解

    这篇文章主要为大家介绍了MPAndroidChart绘制自定义运动数据图表示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Flutter路由的几种用法小结

    Flutter路由的几种用法小结

    这篇文章主要介绍了Flutter路由的几种用法,包括基本路由跳转和路由跳转传参方法,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-12-12

最新评论