第三方登陆QQ完整版

在这个网址里下载  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

清单文件中注册

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.lianxiday07_disanfang">  
  4.   
  5.     <!-- QQ登录授权所需权限 -->  
  6.     <uses-permission android:name="android.permission.INTERNET" />  
  7.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  8.     <application  
  9.         android:allowBackup="true"  
  10.         android:icon="@mipmap/ic_launcher"  
  11.         android:label="@string/app_name"  
  12.         android:supportsRtl="true"  
  13.         android:theme="@style/AppTheme">  
  14.         <activity android:name=".MainActivity">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.   
  22.         <!-- 注册SDKActivity -->  
  23.         <activity  
  24.             android:name="com.tencent.tauth.AuthActivity"  
  25.             android:launchMode="singleTask"  
  26.             android:noHistory="true" >  
  27.             <intent-filter>  
  28.                 <action android:name="android.intent.action.VIEW" />  
  29.                 <category android:name="android.intent.category.DEFAULT" />  
  30.                 <category android:name="android.intent.category.BROWSABLE" />  
  31.                 <data android:scheme="tencent1105602574" /> <!-- 开放平台获取的APPID -->  
  32.             </intent-filter>  
  33.         </activity>  
  34.         <activity android:name="com.tencent.connect.common.AssistActivity"  
  35.   
  36.             android:screenOrientation="portrait"/>  
  37.     </application>  
  38.   
  39. </manifest>  

activity_main的布局

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/activity_main"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.   >  
  7.   
  8.     <Button  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="点击QQ登录"  
  12.         android:onClick="buttonLogin"  
  13.         android:layout_centerInParent="true"  
  14.         android:textSize="16sp"  
  15.         android:textColor="#f4736e"/>  
  16. </RelativeLayout>  

MainActivity的代码

 
 
[html] view plain copy
  1. public class MainActivity extends AppCompatActivity {  
  2.     private static final String TAG = "MainActivity";  
  3.     private static final String APP_ID = "1105602574";//官方获取的APPID  
  4.     private Tencent mTencent;  
  5.     private BaseUiListener mIUiListener;  
  6.     private UserInfo mUserInfo;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.   
  12.         //传入参数APPID和全局Context上下文  
  13.         mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());  
  14.     }  
  15.   
  16.     public void buttonLogin(View v){  
  17.         /**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限  
  18.          官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all”  
  19.          第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */  
  20.         mIUiListener = new BaseUiListener();  
  21.         //all表示获取所有权限  
  22.         mTencent.login(MainActivity.this,"all", mIUiListener);  
  23.     }  
  24.   
  25.     /**  
  26.      * 自定义监听器实现IUiListener接口后,需要实现的3个方法  
  27.      * onComplete完成 onError错误 onCancel取消  
  28.      */  
  29.     private class BaseUiListener implements IUiListener {  
  30.   
  31.         @Override  
  32.         public void onComplete(Object response) {  
  33.             Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();  
  34.             Log.e(TAG, "response:" + response);  
  35.             JSONObject obj = (JSONObject) response;  
  36.             try {  
  37.                 String openID = obj.getString("openid");  
  38.                 String accessToken = obj.getString("access_token");  
  39.                 String expires = obj.getString("expires_in");  
  40.                 mTencent.setOpenId(openID);  
  41.                 mTencent.setAccessToken(accessToken,expires);  
  42.                 QQToken qqToken = mTencent.getQQToken();  
  43.                 mUserInfo = new UserInfo(getApplicationContext(),qqToken);  
  44.                 mUserInfo.getUserInfo(new IUiListener() {  
  45.                     @Override  
  46.                     public void onComplete(Object response) {  
  47.                         Log.e(TAG,"登录成功"+response.toString());  
  48.                     }  
  49.   
  50.                     @Override  
  51.                     public void onError(UiError uiError) {  
  52.                         Log.e(TAG,"登录失败"+uiError.toString());  
  53.                     }  
  54.   
  55.                     @Override  
  56.                     public void onCancel() {  
  57.                         Log.e(TAG,"登录取消");  
  58.   
  59.                     }  
  60.                 });  
  61.             } catch (Exception e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.         }  
  65.   
  66.         @Override  
  67.         public void onError(UiError uiError) {  
  68.             Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();  
  69.   
  70.         }  
  71.   
  72.         @Override  
  73.         public void onCancel() {  
  74.             Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();  
  75.   
  76.         }  
  77.   
  78.     }  
  79.   
  80.     /**  
  81.      * 在调用Login的Activity或者Fragment中重写onActivityResult方法  
  82.      * @param requestCode  
  83.      * @param resultCode  
  84.      * @param data  
  85.      */  
  86.     @Override  
  87.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  88.         if(requestCode == Constants.REQUEST_LOGIN){  
  89.             Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);  
  90.         }  
  91.         super.onActivityResult(requestCode, resultCode, data);  
  92.     }  
  93.   
  94. }  
授权成功后跳转到的个人中心页面的布局
[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     android:gravity="center"  
  7.    >  
  8.   
  9.     <TextView  
  10.         android:textSize="23sp"  
  11.         android:padding="20dp"  
  12.         android:text="个人信息"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content" />  
  15.   
  16.     <TextView  
  17.         android:background="#000000"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="1dp" />  
  20.   
  21.     <LinearLayout  
  22.         android:padding="10dp"  
  23.         android:gravity="center_vertical"  
  24.         android:orientation="horizontal"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content">  
  27.   
  28.         <TextView  
  29.             android:layout_weight="1"  
  30.             android:layout_width="0dp"  
  31.             android:layout_height="wrap_content"  
  32.             android:textSize="23sp"  
  33.             android:text="头像"  
  34.             />  
  35.   
  36.         <ImageView  
  37.             android:layout_marginLeft="200dp"  
  38.             android:src="@drawable/_tou"  
  39.             android:layout_width="0dp"  
  40.             android:layout_weight="1"  
  41.             android:layout_height="80dp" />  
  42.   
  43.     </LinearLayout>  
  44.     <TextView  
  45.         android:background="#000000"  
  46.         android:layout_width="match_parent"  
  47.         android:layout_height="1dp" />  
  48.   
  49.     <LinearLayout  
  50.         android:paddingLeft="10dp"  
  51.         android:paddingTop="20dp"  
  52.         android:paddingBottom="20dp"  
  53.         android:gravity="center_vertical"  
  54.         android:orientation="horizontal"  
  55.         android:layout_width="match_parent"  
  56.         android:layout_height="wrap_content">  
  57.   
  58.         <TextView  
  59.             android:layout_width="0dp"  
  60.             android:layout_weight="1"  
  61.             android:layout_height="wrap_content"  
  62.             android:textSize="23sp"  
  63.             android:text="用户名"  
  64.             />  
  65.   
  66.         <TextView  
  67.             android:layout_marginLeft="170dp"  
  68.             android:layout_width="0dp"  
  69.             android:layout_weight="1"  
  70.             android:layout_height="wrap_content"  
  71.             android:textSize="23sp"  
  72.             android:text="username"  
  73.             />  
  74.   
  75.   
  76.   
  77.     </LinearLayout>  
  78.     <TextView  
  79.         android:background="#000000"  
  80.         android:layout_width="match_parent"  
  81.         android:layout_height="1dp" />  
  82.   
  83.     <LinearLayout  
  84.         android:paddingLeft="10dp"  
  85.         android:paddingTop="20dp"  
  86.         android:paddingBottom="20dp"  
  87.         android:gravity="center_vertical"  
  88.         android:orientation="horizontal"  
  89.         android:layout_width="match_parent"  
  90.         android:layout_height="wrap_content">  
  91.   
  92.         <TextView  
  93.             android:layout_width="0dp"  
  94.             android:layout_weight="1"  
  95.             android:layout_height="wrap_content"  
  96.             android:textSize="23sp"  
  97.             android:text="性别"  
  98.             />  
  99.   
  100.         <TextView  
  101.             android:layout_marginLeft="270dp"  
  102.             android:layout_width="0dp"  
  103.             android:layout_weight="1"  
  104.             android:layout_height="wrap_content"  
  105.             android:textSize="23sp"  
  106.             android:text="女"  
  107.             />  
  108.   
  109.       </LinearLayout>  
  110.     <TextView  
  111.         android:background="#000000"  
  112.         android:layout_width="match_parent"  
  113.         android:layout_height="1dp" />  
  114.     <Button  
  115.         android:textSize="23sp"  
  116.         android:layout_marginTop="150dp"  
  117.         android:text="退出登录"  
  118.         android:layout_width="200dp"  
  119.         android:layout_height="60dp" />  
  120. </LinearLayout> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值