Android实现多渠道分享的统一回调
渠道分享这个功能几乎每个App都存在,这个是App推广的重要手段。可能大家会用第三方的分享SDK,简单易用,稳定性高。但是有时候出于安全的考虑,可能需要实现自己独立的分享模块。下面就简单介绍下实现分享统一回调的思想。因为保密的需要,这里不方便公开源代码,有兴趣可以留言讨论。
下面以微信和QQ为例简单介绍下实现思路。
1.QQ分享
QQ分享的官网:http://wiki.open.qq.com/wiki/分享消息到QQ(定向分享)
大家看过QQ的分享应该知道QQ的回调需要在onActivityResult里,当然需要实现IUiListener,这个具体的可以看下上面的链接。
重点就是需要实现回调。
2.微信分享
微信分享官网:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN
微信的回调WXhandlerActivity这个要实现,回调可以在里面也可以给他一个父类,在父类里实现回调。
3.实现
Android实现的分享需要界面,说以Activity肯定是要实现的。我的思路就是定义一个ShareActivity,分享的界面在这里面实现,同时回调也由ShareActivity来实现。另外需要一定义一个工具类来处理数据,调用ShareActivity,定义为ShareUtil。ShareUtil里面实现调用ShareActivity,接收分享的数据。
ShareUtil的结构:
public static void share(Context context, ShareInfo shareInfo ) {
startShareActivity(context, shareInfo, ACTION_OPEN, shareInfo.getShareLogoBytes(),null);
}
public static void sharePanel(Context context, ShareInfo shareInfo ) {
startShareActivity(context, shareInfo, ACTION_PANEL, shareInfo.getShareLogoBytes(),null);
}
public static void startShareActivity(Context context, ShareInfo shareInfo, int action, byte[] bytes,SharePlatformListener sharePlatformListener) {
if (!isColdDown()) {
ShareActivity.sharePlatformListener=sharePlatformListener;
init();
Intent intent =new Intent();
intent.setAction(SHARE_ACTIVITY);
intent.putExtra(SHAREINFO, shareInfo);
intent.putExtra(SHARE_ACT_ACTION, action);
intent.putExtra(SHARE_BYTES, bytes);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
public static void backShareActivity(Context context, int result, String transaction, String msg) {
Intent intent =new Intent(SHARE_ACTIVITY);
intent.putExtra(SHARE_ACT_ACTION, ACTION_BACK);
intent.putExtra(SHARE_ACT_RESULT, result);
intent.putExtra(SHARE_ACT_TRANS, transaction);
intent.putExtra(SHARE_ACT_MSG, msg);
context.startActivity(intent);
}
这里实现了直接分享跟基本的九宫格分享。
ShareActivity结构:
private BaseUiListenermBaseUiListener;
public static SharePlatformListenersharePlatformListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
initData(intent);
thisActivity =this;
if (mAction == ShareUtil.ACTION_BACK) {//分享回调
mTrans = intent.getStringExtra(ShareUtil.SHARE_ACT_TRANS);
mSharedResult = intent.getIntExtra(ShareUtil.SHARE_ACT_RESULT, 0);
setSharedResult(mSharedResult, mTrans, mSharedMsg);//分享结果
finish();
return;
}
if (mShareInfo ==null &&mShareInfo.getShareImageInfo() ==null) {
finish();
return;
}
setContentView(R.layout.share_activity);
mRootView = (RelativeLayout) findViewById(R.id.share_activity);
mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(mSelectedChannel)) {
finish();
}
}
});
mSelectedChannel =mShareInfo.getChannels();
if (TextUtils.isEmpty(mSelectedChannel) &&mAction != ShareUtil.ACTION_PANEL) {
return;
}
if (mSelectedChannel.startsWith(ShareUtil.Wechat) && ((TextUtils.isEmpty(mShareInfo.getIconUrl()) &&mShareInfo.getShareImageInfo() ==null)||"jdunion_tran".equals(mShareInfo.getEventName())) && !TextUtils.isEmpty(mShareInfo.getSummary())) {//只分享文字只支持微信
doShareMessage();
return;
}
setShareInfo();
if (mAction == ShareUtil.ACTION_OPEN) {//直接分享
if (mShareInfo.getShareImageInfo() !=null && !TextUtils.isEmpty(mShareInfo.getShareImageInfo().directPath)) {//图片存在分享图片
mBitmapSavedPath =mShareInfo.getShareImageInfo().directPath;
doShareImage();
}else {
doShare();
}
}else if (mAction == ShareUtil.ACTION_PANEL) {//九宫格分享
showPanel();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {//QQ分享回调
if (QQUtil.getTencentInstance() !=null) {
QQUtil.getTencentInstance().onActivityResultData(
requestCode, resultCode, intent, mBaseUiListener);
}
}
public void setSharedResult(int result, String transaction, String msg) {//处理回调结果
mSharedResult = result;
mSharedMsg = msg;
splitTransaction(transaction);
String message="";
switch(result){
case ShareUtil.RESULT_CANCEL:
if(sharePlatformListener!=null){
sharePlatformListener.onCancel(mSharedChannel);
}else{
message="分享取消";
}
break;
case ShareUtil.RESULT_ERROR:
if(sharePlatformListener!=null){
sharePlatformListener.onError(mSharedChannel,mSharedMsg);
}else{
message="分享失败";
}
break;
case ShareUtil.RESULT_SUCCESS:
if(sharePlatformListener!=null){
sharePlatformListener.onComplete(mSharedChannel);
}else{
message="分享成功";
}
break;
default:message="分享错误";
}
if(!TextUtils.isEmpty(message)){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
finish();
}
//QQ回调
private class BaseUiListenerimplements IUiListener {
public Stringtransaction; // 分享事务ID,作为回调凭证
@Override
public void onComplete(Object obj) {
setSharedResult(RESULT_SUCCESS, RESULT_SUCCESS+"##"+ShareUtil.QQ, "success");
}
@Override
public void onError(UiError e) {
setSharedResult(RESULT_ERROR, RESULT_ERROR+"##"+ShareUtil.QQ, e.errorMessage);
}
@Override
public void onCancel() {
setSharedResult(RESULT_CANCEL, RESULT_CANCEL+"##"+ShareUtil.QQ, "cancel");
}
}
WXhandlerActivity核心代码:
@Override
public void onReq(BaseReq req) {
if (Log.D) {
Log.d(TAG, " onReq -->> ");
}
WeiXinManager.getInstance().receivedReq(req);
}
@Override
public void onResp(BaseResp resp) {
if (Log.D) {
Log.d(TAG, " onResp -->> resp.errCode : " + resp.errCode);
}
switch (resp.getType()) {
case ConstantsAPI.COMMAND_SENDAUTH:
handleWXLoginResp(resp);
break;
case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
//发出分享时设定的此值,作为判定是分享回调的依据
if (TextUtils.isEmpty(resp.transaction))break;
int result = -1;
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:// 分享成功
result = ShareUtil.RESULT_SUCCESS;
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:// 分享取消
result = ShareUtil.RESULT_CANCEL;
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:// 分享失败
result = ShareUtil.RESULT_ERROR;
break;
default:
ToastUtils.showToast(resp.errCode +":" + resp.errStr);
}
ShareUtil.backShareActivity(this, result, resp.transaction, resp.errStr);
break;
default:
break;
}
finish();
}
ShareActivity配置:
4.总结
主要的实现思路就是用一个专门的Activity来实现分享的界面,同时实现回调。定义一个接口来处理回调,具体到业务界面的时候实现这个接口然后根据不同的返回结果给出不同的处理。