关闭

android QQ 登录

428人阅读 评论(0) 收藏 举报
分类:




<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="111111111" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />


    //下面有关QQ参数
    private static final String QQ_APPID = "111111111";
    private Tencent mTencent;
    private IUiListener loginListener;
    private IUiListener userInfoListener;
    private String scope;
    private com.tencent.connect.UserInfo userInfo;
</pre><pre name="code" class="html"> /**
     * QQ登录
     */
    private void QQLogin()
    {
        if (!mTencent.isSessionValid()) {
            mTencent.login(LoginActivity.this, scope, loginListener);
        }
    }

    private void getQQUserInfo()
    {
        if(mTencent.getQQToken() == null){
            System.out.println("qqtoken == null");
        }
        userInfo = new com.tencent.connect.UserInfo(LoginActivity.this, mTencent.getQQToken());
        userInfo.getUserInfo(userInfoListener);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == com.tencent.connect.common.Constants.REQUEST_API) {
            if (resultCode == com.tencent.connect.common.Constants.RESULT_LOGIN) {
                Tencent.handleResultData(data, loginListener);
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }



private void initQQData() {
        mTencent = Tencent.createInstance(QQ_APPID, LoginActivity.this);
        scope = "all";
        loginListener = new IUiListener() {
            @Override
            public void onError(UiError arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onComplete(Object value) {
                // TODO Auto-generated method stub
                if (value == null) {
                    return;
                }
                try {
                    JSONObject jo = (JSONObject) value;
                    int ret = jo.getInt("ret");
                    UpMiLog.d(TAG, "json=" + String.valueOf(jo));
                    if (ret == 0) {
                        String openID = jo.getString("openid");
                        String accessToken = jo.getString("access_token");
                        String expires = jo.getString("expires_in");
                        mTencent.setOpenId(openID);
                        mTencent.setAccessToken(accessToken, expires);
                        getQQUserInfo();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
            }
        };
        userInfoListener = new IUiListener() {
            @Override
            public void onError(UiError arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onComplete(Object arg0) {
                // TODO Auto-generated method stub
                if(arg0 == null){
                    return;
                }
                try {
                    JSONObject jo = (JSONObject) arg0;
                    int ret = jo.getInt("ret");
                    UpMiLog.d(TAG,"Get user info json=" + String.valueOf(jo));
                    if(ret == 0){
                        String nickName = jo.getString("nickname");
                        String gender = jo.getString("gender");
                        if (jo.has("figureurl")) {
                            Bitmap bitmap = null;
                            try {
                                String imageUri = jo.getString("figureurl_qq_2");
                                UpMiLog.d(TAG,"QQ head icon=" + imageUri);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else
                        {
                            UpMiLog.d(TAG,"has not head icon");
                        }
                        Toast.makeText(LoginActivity.this, "你好," + nickName, Toast.LENGTH_LONG).show();
                    }else{
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
            }
        };
    }



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:550274次
    • 积分:9740
    • 等级:
    • 排名:第1931名
    • 原创:397篇
    • 转载:29篇
    • 译文:1篇
    • 评论:121条
    最新评论