目前大多数APP都包含了第三方平台的登录,特别是QQ和微信,这篇博客主要讲的是如何实现QQ第三方平台实现QQ登录和分享功能,功能包含:
- 登录授权登录获取用户信息(昵称,头像,地址等)
- QQ分享给好友
- QQ分享到空间
先看看效果图:
要实现以上功能,首先得去腾讯开放平台 注册成为开发者,然后创建应用,得到 APPID,如下图:
名称就是APP名称,可上传Logo图片,授权登录时会显示授权给那个APP,效果如图:
创建成功后,取得APP ID即可,完成以上功能了。
添加这两个jar包
打开我们的Androidmanifest 在里面加入权限和注册Activity 如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shiran.qqlogindemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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" />
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait"/>
</application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
布局文件activity_main 就一个Button按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shiran.qqlogindemo.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击QQ登录"
android:onClick="buttonLogin"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textColor="#f4736e"/>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
下面就是我们的MainActivity中的代码了
package com.shiran.qqlogindemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
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";
private Tencent mTencent;
private BaseUiListener mIUiListener;
private UserInfo mUserInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());
}
public void buttonLogin(View v){
/**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限
官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all”
第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */
mIUiListener = new BaseUiListener();
mTencent.login(MainActivity.this,"all", mIUiListener);
}
/**
* 自定义监听器实现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());
//获得登录用户
JSONObject json = (JSONObject) response;
try {
String figureurl_qq_1 = json.getString("figureurl_qq_1");
// ImageLoader.getInstance().displayImage(figureurl_qq_1,image);
} catch (JSONException e) {
e.printStackTrace();
}
} @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(); } }
/** * 在调用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,mIUiListener); }
super.onActivityResult(requestCode, resultCode, data); }}
//qq分享
private void onClickShare() {
final Bundle params = new Bundle();
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要");
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,
"http://blog.csdn.net/DickyQie/article/list/1");
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,
"http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "切切歆语");
params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
mTencent.shareToQQ(MainActivity.this, params, new shareListener());
}
private void shareToQQzone() {
try {
final Bundle params = new Bundle();
params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "切切歆语");
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "sss");
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,
"http://blog.csdn.net/DickyQie/article/list/1");
ArrayList<String> imageUrls = new ArrayList<String>();
imageUrls.add("http://media-cdn.tripadvisor.com/media/photo-s/01/3e/05/40/the-sandbar-that-links.jpg");
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
params.putInt(QzoneShare.SHARE_TO_QQ_EXT_INT,
QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
Tencent mTencent = Tencent.createInstance("申请的APPID",
MainActivity.this);
mTencent.shareToQzone(MainActivity.this, params,
new shareListener());
} catch (Exception e) {
}
}