这是我们公司自己封装的一套分享框架,不同于市面上的其他第三方分享。
该库暂时只收纳了:微信,QQ,微博等三个平台。
下面就介绍怎么用这个库
- 1、QQ分享资料链接:http://wiki.connect.qq.com/com-tencent-tauth-tencent-sharetoqq
- 2、微信开放平台:https://open.weixin.qq.com/
- 3、新浪微博开放平台:https://github.com/sinaweibosdk/weibo_android_sdk
- 4、新浪微博登录错误码:http://bbs.umeng.com/thread-8348-1-1.html
一、第一步:初始化参数
在需要启动分享的界面对分享的一些对象进行实例化,如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
initParams();
}
private void initParams() {
mShareManger = new CommonShareManager.Builder(this).
registerWeixin(mWeixinAppId).//注册微信
registerQQ(mQQAppId).//注册QQ
//注册微博,注意,这里的第二个参数为redirectUrl(重定向Url)
//这个url必须与在微博开放平台上注册应用时,填写的url一致,否则授权失败。
registerSinaWeibo(mSinaAppkey,"www.baidu.com").
create();
mShareDialog = new CommonShareDialog(this,this);
//如果你的应用有新浪微博时,调用下面方法
//默认是没有的。
mShareDialog.showSina();
}
注:对于上面代码中的注释部分redirectUrl,必须保证其与你在微博上注册应用时填写的url一致,如下图:
二、第二步:写监听回调
让你要用到分享的类实现ICommonShareListener与IUiListener,(注:这两个监听到下一个版本会考虑整成一个,目前由于时间关系,先搞2个)如:
public class SunnyShareActivity extends BaseActivity implements ICommonShareLinstener,IUiListener{
@Override
public void onStartShare(CommonSharePlatform sharePlatform) {
showDialog();
/**
* 音乐
*/
CommonShareMusic music = new CommonShareMusic();
music.setUiListener(this);
music.setTitle(mTitle);
music.setDescription(mDescription);
music.setAppName("SunnyDemo");
music.setTargetUrl(musicUrl);
music.setMusicUrl(musicUrl);
music.setMusicDataUrl(musicDataUrl);
music.setImgUrl(mImgUrl);//这个图片地址不能为null,因为图片大小不能超过32k,所以在底层已经对其进行了压缩处理,下面会分享怎么压缩图片。
music.setSharePlatform(sharePlatform);
/**
* 网页
*/
CommonShareWebpage webpage = new CommonShareWebpage();
webpage.setUiListener(this);
webpage.setTitle(mTitle);
webpage.setDescription(mDescription);
webpage.setAppName("SunnyDemo");
webpage.setTargetUrl("http://news.sina.com.cn/c/2013-10-22/021928494669.shtml");
webpage.setBm(null);
webpage.setSharePlatform(sharePlatform);
/**
* 图片
*/
CommonShareImage image = new CommonShareImage();
image.setUiListener(this);
image.setTitle(mTitle);
image.setDescription(mDescription);
image.setAppName("SunnyDemo2");
image.setTargetUrl(mTargetUrl);
image.setImgUrl(mImgUrl);
image.setSharePlatform(sharePlatform);
/**
* 文字
*/
CommonShareTextOnly textOnly = new CommonShareTextOnly();
textOnly.setUiListener(this);
textOnly.setTitle(mTitle);
textOnly.setDescription(mDescription);
textOnly.setAppName("SunnyDemo2");
textOnly.setTargetUrl(mTargetUrl);
textOnly.setImgUrl(mImgUrl);
textOnly.setSharePlatform(sharePlatform);
//分享文字
//mShareManger.shareTextOnly(textOnly);
//分享音乐
//mShareManger.shareMusic(music);
//分享网页
/* webpage.setImgUrl(mImgUrl);
mShareManger.shareWebpage(webpage);*/
//分享图片
mShareManger.sharePicOnly(image);
}
@Override
public void onShareSuccess(CommonSharePlatform sharePlatform, String msg,CommonShareDialog dialog) {
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
hideDialog();
}
@Override
public void onShareFialure(CommonSharePlatform sharePlatform, String msg) {
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
hideDialog();
}
@Override
public void onShareCancel(CommonSharePlatform sharePlatform, String msg) {
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
hideDialog();
}
/**
* //下面是腾讯分享回调
* @param o
*/
@Override
public void onComplete(Object o) {
Toast.makeText(this,"分享完成",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
Toast.makeText(this,uiError.errorMessage,Toast.LENGTH_SHORT).show();
hideDialog();
}
@Override
public void onCancel() {
Toast.makeText(this,"分享onCancel",Toast.LENGTH_SHORT).show();
}
}
然后在view点击的地方,显示分享对话框
public void startShare(View view){
if(mShareDialog != null && !mShareDialog.isShowing()){
mShareDialog.show();
}
}
三、第三步:实例化CommonShareProxy
在你需要调用分享的Activity中对CommonShareProxy进行实例化,并分享在Activity中的onCreate(),onNewIntent(),onActivityResult()方法中调用commonShareProxy对象的相应方法
public class SunnyShareActivity extends Activity{
private CommonShareProxy commonShareProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
commonShareProxy = new CommonShareProxy(this);
commonShareProxy.onCreate(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
commonShareProxy.onNewIntent(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
commonShareProxy.onActivityResult(requestCode, resultCode, data);
}
}
注:当做新浪微博分享时,需要在AndroidManifest.xml中在进行分享的界面时,由于新浪sdk会根据指定的action去跳转到你传进去的activity,所以需要在配置清单中指定Action,从官方Demo中拷贝action放到指定位置:
代码如下:
<!--分享-->
<activity android:name="sunnydemo2.sharesdk.SunnyShareActivity">
<intent-filter>
<action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
四、关于第三方分享图片大小不能超过32k
在分享中,微信,qq,新浪微博等平台都要求图片不能太大,不能超过32kb,但他们给出的demo中只是简单粗暴的用ic_laucher,对于图片压缩只是一个注释,一笔带过。
下面就分享下图片压缩:
加载网络 图片用Fresco.
//用Fresco获取bitmap
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imgUrl))
.setProgressiveRenderingEnabled(true).build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource =
imagePipeline.fetchDecodedImage(imageRequest, mContext);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(@Nullable Bitmap bitmap) {
if (bitmap != null) {
//要对图片进行压缩,压缩到32kb以下
//采用缩放法压缩
int size = 32;
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
float zoom = (float)Math.sqrt(size * 1024 / (float)out.toByteArray().length);
Matrix matrix = new Matrix();
matrix.setScale(zoom, zoom);
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
while(out.toByteArray().length > size * 1024){
System.out.println(out.toByteArray().length);
matrix.setScale(0.9f, 0.9f);
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
}
bitmap.recycle();
iLoadNetImage.loadImageSuccess(result,null);
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
iLoadNetImage.loadImageFailure();
}
}, CallerThreadExecutor.getInstance());
网上还有其他 的方法,比如,等比例压缩,采样率压缩等都不行,上面采用的是用Matrix缩放,亲测,是可以的。