转载或者分享请注明出处:王亟亟的成长记录
现在第三方登录,等三方授权,分享到朋友圈,微博等等等都是大多数国产App都有的功能,每个应用商都有自己的open平台,希望大家嵌入一些模块。但是人人 微信 微博 开心 易信 啪啪啪一大堆,每一种都去单独嵌入工作量忒大了。所以mob.com的sharesdk就有了它的市场(博主不是推销的黄牛哈,只是用着蛮顺手,客服MM还打电话询问使用的效果啊意见什么的,感觉这家公司给人的感觉还是不错的~~舅舅党勿喷啊!嘿嘿)
废话不说,开始正题!!
贴代码之前先上图,看看是不是你想要的。
OK 开始说流程:
首先你需要去各大平台去注册他们的官方授权,一般为一个AppKey, 一个AppSecret。当然ShareSDK也需要在他们的平台进行注册一个账号,在代码中需要使用的。ShareSdk官方地址,可以下载SDK以及查看一些文档
补充下,你每可能不是每个平台都需要,所以各个平台的Jar包是有给你选择的,你可以选择你需要的jar包进行下载,如下图
解压完下载的压缩包会出现2个文件
名为:ShareSDK for Android的文件里面是等会生成一些lib包和代码用的。
名为:ShareSDK for Android Services的包里面是一个demo的apk如果你想看一下效果,拆解一下功能可以在测试机上面装一下,看看效果。
打开ShareSDK for Android这个文件夹我们需要使用的是 QuickIntegrater.jar这样一个文件,双击后会给我们选择我们需要的java文件啊,lib包啊,xml文件一系列。你只要把他放到你的项目中去就行了。
接下来展示下具体的使用:
红色圈圈部分为微信特有的的一个目录,而且必须有作为回调所需要使用的类,但是你不用去修改什么,按照你在微信开放平台注册的packge路径放就好了
蓝色圈圈部分为 sharesdk存在的配置文件,稍后会为各位细细分析
先贴下重要的ShareSDK.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<DevInfor>
<!--
说明:
1、表格中的第一项
<ShareSDK
AppKey="api20" />
是必须的,其中的AppKey是您在ShareSDK上注册的开发者帐号的AppKey
2、所有集成到您项目的平台都应该为其在表格中填写相对应的开发者信息,以新浪微博为例:
<SinaWeibo
Id="1"
SortId="1"
AppKey="568898243"
AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"
RedirectUrl="http://www.mob.com"
Enable="true" />
其中的SortId是此平台在分享列表中的位置,由开发者自行定义,可以是任何整型数字,数值越大
越靠后AppKey、AppSecret和RedirectUrl是您在新浪微博上注册开发者信息和应用后得到的信息
Id是一个保留的识别符,整型,ShareSDK不使用此字段,供您在自己的项目中当作平台的识别符。
Enable字段表示此平台是否有效,布尔值,默认为true,如果Enable为false,即便平台的jar包
已经添加到应用中,平台实例依然不可获取。
各个平台注册应用信息的地址如下:
新浪微博 http://open.weibo.com
腾讯微博 http://dev.t.qq.com
QQ空间 http://connect.qq.com/intro/login/
微信好友 http://open.weixin.qq.com
Facebook https://developers.facebook.com
Twitter https://dev.twitter.com
人人网 http://dev.renren.com
开心网 http://open.kaixin001.com
搜狐微博 http://open.t.sohu.com
网易微博 http://open.t.163.com
豆瓣 http://developers.douban.com
有道云笔记 http://note.youdao.com/open/developguide.html#app
印象笔记 https://dev.evernote.com/
Linkedin https://developer.linkedin.com
FourSquare https://developer.foursquare.com/
搜狐随身看 https://open.sohu.com/
Flickr http://www.flickr.com/services/
Pinterest http://developers.pinterest.com/
Tumblr http://www.tumblr.com/developers
Dropbox https://www.dropbox.com/developers
Instagram http://instagram.com/developer#
VKontakte http://vk.com/dev
易信好友 http://open.yixin.im/
明道 http://open.mingdao.com/
Line http://media.line.me/zh-hant/
Pocket http://getpocket.com/developer/apps/new
-->
<ShareSDK
AppKey = "api20"/> <!-- 修改成你在sharesdk后台注册的应用的appkey"-->
<Wechat
Id="4"
SortId="4"
AppId="在微信注册的appID"
AppSecret="在微信注册的系列号"
BypassApproval="true"
Enable="true" />
<WechatMoments
Id="5"
SortId="5"
AppId="同分享好友"
AppSecret="同分享好友"
BypassApproval="true"
Enable="true" />
<ShortMessage
Id="13"
SortId="13"
Enable="true" />
</DevInfor>
然后是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sharesdk" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.example.sharesdk.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="cn.sharesdk.framework.ShareSDKUIShell" android:configChanges="keyboardHidden|orientation|screenSize" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:windowSoftInputMode="stateHidden|adjustResize" />
<!-- 微信分享回调 -->
<activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>
</manifest>
接下来是具体分享所用到的类:
1>弹窗的显示分享类:
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;
/** * TODO<分享工具> * * @author Wjj * @data: 2015-5-19 下午4:25:00 * @version: V1.0.9 */
public class SharePopupWindow extends PopupWindow {
private Context context;
private PlatformActionListener platformActionListener;
private ShareParams shareParams;
public SharePopupWindow(Context cx) {
this.context = cx;
}
public PlatformActionListener getPlatformActionListener() {
return platformActionListener;
}
public void setPlatformActionListener(
PlatformActionListener platformActionListener) {
this.platformActionListener = platformActionListener;
}
public void showShareWindow() {
View view = LayoutInflater.from(context).inflate(R.layout.share_layout,
null);
GridView gridView = (GridView) view.findViewById(R.id.share_gridview);
ShareAdapter adapter = new ShareAdapter(context);
gridView.setAdapter(adapter);
Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
// 取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});
// 设置SelectPicPopupWindow的View
this.setContentView(view);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
// 设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
gridView.setOnItemClickListener(new ShareItemClickListener(this));
}
private class ShareItemClickListener implements OnItemClickListener {
private PopupWindow pop;
public ShareItemClickListener(PopupWindow pop) {
this.pop = pop;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
share(position);
pop.dismiss();
}
}
/** * 分享 * * @param position */
private void share(int position) {
if (position == 1) {
qq();
} else if (position == 4) {
qzone();
} else if(position==5){
shortMessage();
}else{
Platform plat = null;
plat = ShareSDK.getPlatform(context, getPlatform(position));
if (platformActionListener != null) {
plat.setPlatformActionListener(platformActionListener);
}
plat.share(shareParams);
}
}
/** * 初始化分享参数 * * @param shareModel */
public void initShareParams(ShareModel shareModel) {
if (shareModel != null) {
ShareParams sp = new ShareParams();
sp.setShareType(Platform.SHARE_TEXT);
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setTitle(shareModel.getText());
sp.setText(shareModel.getText());
sp.setUrl(shareModel.getUrl());
sp.setImageUrl(shareModel.getImageUrl());
shareParams = sp;
}
}
/** * 获取平台 * * @param position * @return */
private String getPlatform(int position) {
String platform = "";
switch (position) {
case 0:
platform = "Wechat";
break;
case 1:
platform = "QQ";
break;
case 2:
platform = "SinaWeibo";
break;
case 3:
platform = "WechatMoments";
break;
case 4:
platform = "QZone";
break;
case 5:
platform = "ShortMessage";
break;
}
return platform;
}
/** * 分享到QQ空间 */
private void qzone() {
ShareParams sp = new ShareParams();
sp.setTitle(shareParams.getTitle());
sp.setTitleUrl(shareParams.getUrl()); // 标题的超链接
sp.setText(shareParams.getText());
sp.setImageUrl(shareParams.getImageUrl());
sp.setComment("我对此分享内容的评论");
sp.setSite(shareParams.getTitle());
sp.setSiteUrl(shareParams.getUrl());
Platform qzone = ShareSDK.getPlatform(context, "QZone");
qzone.setPlatformActionListener(platformActionListener); // 设置分享事件回调 //
// 执行图文分享
qzone.share(sp);
}
private void qq() {
ShareParams sp = new ShareParams();
sp.setTitle(shareParams.getTitle());
sp.setTitleUrl(shareParams.getUrl()); // 标题的超链接
sp.setText(shareParams.getText());
sp.setImageUrl(shareParams.getImageUrl());
sp.setComment("我对此分享内容的评论");
sp.setSite(shareParams.getTitle());
sp.setSiteUrl(shareParams.getUrl());
Platform qq = ShareSDK.getPlatform(context, "QQ");
qq.setPlatformActionListener(platformActionListener);
qq.share(sp);
}
/** * 分享到短信 */
private void shortMessage() {
ShareParams sp = new ShareParams();
sp.setAddress("");
sp.setText(shareParams.getText()+"这是网址《"+shareParams.getUrl()+"》很给力哦!");
Platform circle = ShareSDK.getPlatform(context, "ShortMessage");
circle.setPlatformActionListener(platformActionListener); // 设置分享事件回调
// 执行图文分享
circle.share(sp);
}
}
2>相应布局的适配器
/* * @Title: ShareAdapter.java * @Copyright: XXX Co., Ltd. Copyright YYYY-YYYY, All rights reserved * @Description: TODO<请描述此文件是做什么的> * @author Wjj * @data: 2015-5-19 下午4:25:00 * @version: V1.0.9 */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/** * TODO< 分享弹出框Adapter > * @author Wjj * @data: 2015-5-19 下午4:25:00 * @version: V1.0.9 */
public class ShareAdapter extends BaseAdapter {
private static String[] shareNames = new String[] {"微信", "QQ", "微博","朋友圈", "QQ空间", "短信"};
private int[] shareIcons = new int[] {R.drawable.sns_weixin_icon, R.drawable.sns_qqfriends_icon, R.drawable.sns_sina_icon,R.drawable.sns_weixin_timeline_icon,
R.drawable.sns_qzone_icon,R.drawable.short_message_nor};
private LayoutInflater inflater;
public ShareAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
return shareNames.length;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = inflater.inflate(R.layout.share_item, null);
}
ImageView shareIcon = (ImageView) convertView.findViewById(R.id.share_icon);
TextView shareTitle = (TextView) convertView.findViewById(R.id.share_title);
shareIcon.setImageResource(shareIcons[position]);
shareTitle.setText(shareNames[position]);
return convertView;
}
}
3>作为填充对象的类:
/** * * TODO<分享内容参数> * * @author Wjj * @data: 2015-5-19 下午4:25:00 * @version: V1.0.9 */
public class ShareModel {
private String title;
private String text;
private String url;
private String imageUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
主Main方法
import java.util.HashMap;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity implements PlatformActionListener, Callback {
private String text = "测试用的文字,好吧,还是要长一点,欢迎+qq 452270579互相学习";
private String imageurl = "http://survey.isengine.com.cn:8002/upload/img/ios/3.png";
private String title = "标题啦!!";
private String url = "www.baidu.com";
public static final String SHARE_APP_KEY = "21b0f35691b8";
private Button shareButton;
private SharePopupWindow share;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShareSDK.initSDK(this);
shareButton = (Button) findViewById(R.id.share);
shareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
share = new SharePopupWindow(MainActivity.this);
share.setPlatformActionListener(MainActivity.this);
ShareModel model = new ShareModel();
model.setImageUrl(imageurl);
model.setText(text);
model.setTitle(title);
model.setUrl(url);
share.initShareParams(model);
share.showShareWindow();
// 显示窗口 (设置layout在PopupWindow中显示的位置)
share.showAtLocation(
MainActivity.this.findViewById(R.id.main),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (share != null) {
share.dismiss();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ShareSDK.stopSDK(this);
}
@Override
public void onCancel(Platform arg0, int arg1) {
Message msg = new Message();
msg.what = 0;
UIHandler.sendMessage(msg, this);
}
@Override
public void onComplete(Platform plat, int action,
HashMap<String, Object> res) {
Message msg = new Message();
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = plat;
UIHandler.sendMessage(msg, this);
}
@Override
public void onError(Platform arg0, int arg1, Throwable arg2) {
Message msg = new Message();
msg.what = 1;
UIHandler.sendMessage(msg, this);
}
@Override
public boolean handleMessage(Message msg) {
int what = msg.what;
if (what == 1) {
Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
}
if (share != null) {
share.dismiss();
}
return false;
}
}
所用到的一些内容大致就是这些,具体的还是下载代码看吧。
代码地址 访问密码 68f1
有问题欢迎QQ 452270579联系或者邮箱[email protected]