随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。
操作步骤:
1、在share官网注册应用生成AppKey并下载SDK 网址:http://www.shareSDK.cn
2、在QQ空间平台注册应用生成AppId和AppKey 网址:http://connect.qq.com/intro/login/
3、导入SDK包。只实现QQ空间登录分享的话只需要导入ShareSDK-Core.jar(核心包)、ShareSDK-QZone.jar(QQ空间)、cn.sharesdk.onekeyshare.jar(快捷分享)
4、配置:需要配置assets里面的ShareSDKDevInfor.xml和AndroidManifest.xml(后面有示例介绍)
5、添加代码实现登录、获取信息、分享等功能(见下面示例)。
ShareSDKDevInfor.xml:
AndroidManifest.xml:
LoginActivity:
package com.home.testqqshare;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.tencent.qzone.QZone;
public class LoginActivity extends Activity implements OnClickListener,
WeiboActionListener, Callback {
private Button loginBtn;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// 初始化ShareSDK
AbstractWeibo.initSDK(this);
loginBtn = (Button) findViewById(R.id.login_btn_QQ);
loginBtn.setOnClickListener(this);
handler = new Handler(this);
}
@Override
public void onClick(View v) {
if (v == loginBtn) {
AbstractWeibo weibo = AbstractWeibo.getWeibo(this, QZone.NAME);
weibo.setWeiboActionListener(this);
weibo.showUser(null);
}
}
@Override
public void onComplete(AbstractWeibo weibo, int arg1,
HashMap arg2) {
Message msg = new Message();
msg.arg1 = 1;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public void onError(AbstractWeibo weibo, int arg1, Throwable t) {
t.printStackTrace();
Message msg = new Message();
msg.arg1 = 2;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public void onCancel(AbstractWeibo weibo, int arg1) {
Message msg = new Message();
msg.arg1 = 3;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
protected void onDestroy() {
// 结束ShareSDK的统计功能并释放资源
AbstractWeibo.stopSDK(this);
super.onDestroy();
}
/**
* 处理从授权页面返回的结果:异常则给出提示,正常则跳转页面
*
*/
@Override
public boolean handleMessage(Message msg) {
AbstractWeibo weibo = (AbstractWeibo) msg.obj;
switch (msg.arg1) {
case 1: { // 成功
Toast.makeText(this, weibo.getName() + "授权成功", Toast.LENGTH_SHORT)
.show();
}
break;
case 2: { // 失败
Toast.makeText(this, weibo.getName() + "授权失败", Toast.LENGTH_SHORT)
.show();
return false;
}
case 3: { // 取消
Toast.makeText(this, weibo.getName() + "授权取消", Toast.LENGTH_SHORT)
.show();
return false;
}
}
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
return false;
}
}
MainActivity:
package com.home.testqqshare;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.onekeyshare.ShareAllGird;
import cn.sharesdk.tencent.qzone.QZone;
public class MainActivity extends Activity implements OnClickListener,
WeiboActionListener, Callback {
private Button getInfoBtn;
private Button noUIShareBtn;
private Button hasUIShareBtn;
private Button logoutBtn;
private Handler handler;
// 定义图片存放的地址
public static String TEST_IMAGE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AbstractWeibo.initSDK(this);
initWidget();
initImage();
}
/**
* 初始化UI相关组件
*/
private void initWidget() {
getInfoBtn = (Button) findViewById(R.id.main_get_userinfo);
noUIShareBtn = (Button) findViewById(R.id.main_share_noUI);
hasUIShareBtn = (Button) findViewById(R.id.main_share_hasUI);
logoutBtn = (Button) findViewById(R.id.main_btn_logout);
getInfoBtn.setOnClickListener(this);
noUIShareBtn.setOnClickListener(this);
hasUIShareBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
handler = new Handler(this);
}
@Override
public void onClick(View v) {
if (v == getInfoBtn) {
AbstractWeibo weibo = AbstractWeibo.getWeibo(this, QZone.NAME);
weibo.setWeiboActionListener(this);
weibo.showUser(null);
} else if (v == noUIShareBtn) {
showGrid(true);
} else if (v == hasUIShareBtn) {
showGrid(false);
} else if (v == logoutBtn) {// 注销
AbstractWeibo weibo = AbstractWeibo.getWeibo(this, QZone.NAME);
if (weibo.isValid()) {
weibo.removeAccount();
finish();
}
}
}
@Override
public void onCancel(AbstractWeibo weibo, int arg1) {
Message msg = new Message();
msg.arg1 = 3;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public void onComplete(AbstractWeibo weibo, int arg1,
HashMap res) {
Message msg = new Message();
msg.arg1 = 1;
JsonUtils ju = new JsonUtils();
String json = ju.fromHashMap(res);
msg.obj = ju.format(json);
handler.sendMessage(msg);
}
@Override
public void onError(AbstractWeibo weibo, int arg1, Throwable arg2) {
Message msg = new Message();
msg.arg1 = 2;
msg.obj = weibo;
handler.sendMessage(msg);
}
/** 处理操作结果 */
public boolean handleMessage(Message msg) {
switch (msg.arg1) {
case 1: { // 成功
Intent intent = new Intent(MainActivity.this,
ShowInfoActivity.class);
intent.putExtra("data", String.valueOf(msg.obj));
startActivity(intent);
}
break;
case 2: {// 失败
AbstractWeibo weibo = (AbstractWeibo) msg.obj;
Toast.makeText(this, weibo.getName() + "授权失败", Toast.LENGTH_SHORT)
.show();
}
break;
case 3: {// 取消
AbstractWeibo weibo = (AbstractWeibo) msg.obj;
Toast.makeText(this, weibo.getName() + "授权取消", Toast.LENGTH_SHORT)
.show();
}
break;
}
return false;
}
/**
* 使用快捷分享完成图文分享
*
* @param silent
* 是否直接分享
*/
private void showGrid(boolean silent) {
Intent i = new Intent(this, ShareAllGird.class);
// 分享时Notification的图标
i.putExtra("notif_icon", R.drawable.ic_launcher);
// 分享时Notification的标题
i.putExtra("notif_title", this.getString(R.string.app_name));
// title标题,在印象笔记、邮箱、信息、微信(包括好友和朋友圈)、人人网和QQ空间使用,否则可以不提供
i.putExtra("title", this.getString(R.string.share));
// titleUrl是标题的网络链接,仅在人人网和QQ空间使用,否则可以不提供
i.putExtra("titleUrl", "http://sharesdk.cn");
// text是分享文本,所有平台都需要这个字段
i.putExtra("text", this.getString(R.string.share_content));
// imagePath是本地的图片路径,所有平台都支持这个字段,不提供,则表示不分享图片
i.putExtra("imagePath", MainActivity.TEST_IMAGE);
// url仅在微信(包括好友和朋友圈)中使用,否则可以不提供
i.putExtra("url", "http://sharesdk.cn");
// thumbPath是缩略图的本地路径,仅在微信(包括好友和朋友圈)中使用,否则可以不提供
i.putExtra("thumbPath", MainActivity.TEST_IMAGE);
// appPath是待分享应用程序的本地路劲,仅在微信(包括好友和朋友圈)中使用,否则可以不提供
i.putExtra("appPath", MainActivity.TEST_IMAGE);
// comment是我对这条分享的评论,仅在人人网和QQ空间使用,否则可以不提供
i.putExtra("comment", this.getString(R.string.share));
// site是分享此内容的网站名称,仅在QQ空间使用,否则可以不提供
i.putExtra("site", this.getString(R.string.app_name));
// siteUrl是分享此内容的网站地址,仅在QQ空间使用,否则可以不提供
i.putExtra("siteUrl", "http://sharesdk.cn");
// 是否直接分享
i.putExtra("silent", silent);
this.startActivity(i);
}
/**
* 初始化分享的本地图片
*/
private void initImage() {
try {// 判断SD卡中是否存在此文件夹
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
File baseFile = new File(
Environment.getExternalStorageDirectory(), "share");
if (!baseFile.exists()) {
baseFile.mkdir();
}
TEST_IMAGE = baseFile.getAbsolutePath() + "/picture.png";
} else {
TEST_IMAGE = getApplication().getFilesDir().getAbsolutePath()
+ "/picture.png";
}
File file = new File(TEST_IMAGE);
// 判断图片是否存此文件夹中
if (!file.exists()) {
file.createNewFile();
Bitmap pic = BitmapFactory.decodeResource(getResources(),
R.drawable.picture);
FileOutputStream fos = new FileOutputStream(file);
pic.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
} catch (Throwable t) {
t.printStackTrace();
TEST_IMAGE = null;
}
}
}
显示信息ShowInfoActivity:
package com.home.testqqshare;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowInfoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_info);
TextView show = (TextView) findViewById(R.id.show_info_tv);
show.setText(getIntent().getStringExtra("data"));
}
}
JsonUtils工具类:
package com.home.testqqshare;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtils {
/**
* 将指定的json数据转成 HashMap对象
*/
public HashMap fromJson(String jsonStr) {
try {
if (jsonStr.startsWith("[") && jsonStr.endsWith("]")) {
jsonStr = "{\"fakelist\":" + jsonStr + "}";
}
JSONObject json = new JSONObject(jsonStr);
return fromJson(json);
} catch (Throwable t) {
t.printStackTrace();
}
return new HashMap();
}
private HashMap fromJson(JSONObject json)
throws JSONException {
HashMap map = new HashMap();
@SuppressWarnings("unchecked")
Iterator iKey = json.keys();
while (iKey.hasNext()) {
String key = iKey.next();
Object value = json.opt(key);
if (JSONObject.NULL.equals(value)) {
value = null;
}
if (value != null) {
if (value instanceof JSONObject) {
value = fromJson((JSONObject) value);
} else if (value instanceof JSONArray) {
value = fromJson((JSONArray) value);
}
map.put(key, value);
}
}
return map;
}
private ArrayList
登录界面布局文件login.xml:
主布局文件main.xml:
显示信息的布局show_info.xml:
strings:
TestQQShare
Settings
Hello world!
测试使用shareSDK进行分享,感觉还不错,使用方便,功能强大!
分享
DEMO下载地址:http://download.csdn.net/detail/u010142437/6885943