一、集成步骤如下:
1、在ShareSDK官网下载最新的集成包,如:ShareSDK-Android-v2.4.0-All.rar
2、点击QuickIntegrater工具,通过输入项目名称和包名,快速集成第三方资源文件,将这些资源文件复制到自己的项目中。
3、项目相关:
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thirddemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.thirddemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.thirddemo.SecondActivity" >
</activity>
<activity
android:name="cn.sharesdk.framework.ShareSDKUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/qqIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="@drawable/logo_qq" />
<ImageView
android:id="@+id/sinaIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="@drawable/logo_sinaweibo" />
<ImageView
android:id="@+id/facebookIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="@drawable/logo_facebook" />
<ImageView
android:id="@+id/googleIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="@drawable/logo_googleplus" />
<ImageView
android:id="@+id/twitterIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="@drawable/logo_twitter" />
</LinearLayout>
MainActivity.java
package com.example.thirddemo2;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.google.GooglePlus;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.twitter.Twitter;
public class MainActivity extends Activity implements OnClickListener, PlatformActionListener, Callback {
private static final int MSG_USERID_FOUND = 1;//用户已经存在
private static final int MSG_LOGIN = 2;//登录中
private static final int MSG_AUTH_CANCEL = 3;//取消授权
private static final int MSG_AUTH_ERROR = 4;//授权出错
private static final int MSG_AUTH_COMPLETE = 5;//授权成功
private ImageView qqIV, sinaIV, googleIV, facebookIV, twitterIV;
private String userinfo;//存放用户信息
private HashMap<String, Object> mapQQ = new HashMap<String, Object>();//授权信息
private HashMap<String, Object> mapSina = new HashMap<String, Object>();//授权信息
private HashMap<String, Object> mapFB = new HashMap<String, Object>();//授权信息
private HashMap<String, Object> mapGG = new HashMap<String, Object>();//授权信息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShareSDK.initSDK(this);
setContentView(R.layout.activity_main);
initView();
}
//一定要停止
@Override
protected void onDestroy() {
ShareSDK.stopSDK(this);
super.onDestroy();
}
private void initView() {
qqIV = (ImageView) this.findViewById(R.id.qqIV);
qqIV.setOnClickListener(this);
sinaIV = (ImageView) this.findViewById(R.id.sinaIV);
sinaIV.setOnClickListener(this);
facebookIV = (ImageView) this.findViewById(R.id.facebookIV);
facebookIV.setOnClickListener(this);
googleIV = (ImageView) this.findViewById(R.id.googleIV);
googleIV.setOnClickListener(this);
twitterIV = (ImageView) this.findViewById(R.id.twitterIV);
twitterIV.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.qqIV:
authorize(new QQ(this));
break;
case R.id.sinaIV:
authorize(new SinaWeibo(this));
break;
case R.id.facebookIV:
authorize(new Facebook(this));
break;
case R.id.googleIV:
authorize(new GooglePlus(this));
break;
case R.id.twitterIV:
authorize(new Twitter(this));
break;
}
}
/**
* 用于授权
*
* @param plat
*/
private void authorize(Platform plat) {
//如果用户已经授权过,用户信息还存在,将执行if语句
if (plat.isValid()) {
String userId = plat.getDb().getUserId();
if (!TextUtils.isEmpty(userId)) {
UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
if (plat.getName().equals("QQ")) {
login(plat.getName(), userId, mapQQ);
}
else if (plat.getName().equals("SinaWeibo")) {
login(plat.getName(), userId, mapSina);
}
else if (plat.getName().equals("Facebook")) {
login(plat.getName(), userId, mapFB);
}
else if (plat.getName().equals("GooglePlus")) {
login(plat.getName(), userId, mapGG);
}
return;
}
}
//授权监听
plat.setPlatformActionListener(this);
plat.SSOSetting(true);
plat.showUser(null);
}
/**
* 正在登录中
*
* @param plat
* @param userId
* @param userInfo
*/
private void login(String plat, String userId, HashMap<String, Object> userInfo) {
Message msg = new Message();
msg.what = MSG_LOGIN;
msg.obj = plat;
UIHandler.sendMessage(msg, this);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("userinfo", "userinfo:" + userInfo.toString());
startActivity(intent);
}
public void onComplete(Platform platform, int action,
HashMap<String, Object> res) {
if (action == Platform.ACTION_USER_INFOR) {
Log.e("MainActivity", platform.getName());
if (platform.getName().equals("QQ")) {
mapQQ.clear();
mapQQ = res;
}
else if (platform.getName().equals("SinaWeibo")) {
mapSina.clear();
mapSina = res;
}
else if (platform.getName().equals("Facebook")) {
mapFB.clear();
mapFB = res;
}
else if (platform.getName().equals("GooglePlus")) {
mapGG.clear();
mapGG = res;
}
UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
login(platform.getName(), platform.getDb().getUserId(), res);
}
System.out.println(res);
}
public void onError(Platform platform, int action, Throwable t) {
if (action == Platform.ACTION_USER_INFOR) {
UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
}
t.printStackTrace();
}
public void onCancel(Platform platform, int action) {
if (action == Platform.ACTION_USER_INFOR) {
UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
}
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_USERID_FOUND: {
Toast.makeText(this, R.string.userid_found, Toast.LENGTH_SHORT).show();
}
break;
case MSG_LOGIN: {
String text = getString(R.string.logining, msg.obj);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
break;
case MSG_AUTH_CANCEL: {
Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
}
break;
case MSG_AUTH_ERROR: {
Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT).show();
}
break;
case MSG_AUTH_COMPLETE: {
Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
SecondActivity.java
package com.example.thirddemo2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView tv = (TextView) this.findViewById(R.id.tv);
String text = getIntent().getStringExtra("userinfo");
tv.setText(text);
}
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ThirdDemo2</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<integer name="app_version_code">2</integer>
<string name="app_version">2.0.0</string>
<string name="proj_name">ShareSDK_TPL</string>
<string name="share_sdk">Share SDK</string>
<string name="login_with_sina_weibo">用新浪微博登录</string>
<string name="login_with_qzone">用QQ空间登录</string>
<string name="login_with_other_plat">其他帐号登录</string>
<string name="userid_found">用户信息已存在,正在跳转登录操作…</string>
<string name="logining">使用%s帐号登录中…</string>
<string name="auth_cancel">授权操作已取消</string>
<string name="auth_error">授权操作遇到错误,请阅读Logcat输出</string>
<string name="auth_complete">授权成功,正在跳转登录操作…</string>
<string name="if_register_needed">如果您需要注册功能…</string>
<string name="after_auth">一般来说第三方登录时,第三方平台已经为您完成用户身份校验,您可以放心让用户登录。\n\n但如果您需要判断用户是否为新用户,可以在授权操作完成后,将用户的信息通过字段“HashMap userInfo”携带给login方法。客户端通过“String userId”执行登录请求,如果服务端判定此userId为未注册用户,可以将userInfo发送给注册接口,获取用户在此平台上的昵称、头像等数据,然后执行注册</string>
<string name="ok">确定</string>
</resources>
二、测试效果如下:
备注:
其中GooglPlus集成授权失败,不知道哪里有问题,后面发现问题解决方案再补上。