H5混合开发转Android 原生开发,之前也是用的友盟分享、微信原生、QQ原生、微博原生分享,今天这个项目恰好是用的mob的sharesdk分享,老实说,还是第一次使用,一开始接手项目的时候,还以为mob是个后台。
来一张效果图:
如果打算使用分享的话,那么请尽快申请各大平台的appkey、appID等。这里我就不多细说了。
下面主要是如何用sharesdk进行配置
网址:http://www.mob.com/download
(一):选择SDK下载、选择Android选项,选中新浪微博、微信、QQ,点击保存设置。
(二):然后选择右边的下载按钮,这时候会出现一个最新的基于Android Studio的gradle版本配置,只需要简单两步就可以集成进去。
(四):在项目的moudle下的build.gradle添加MobSDK的配置:
MobSDK {
appKey "替换为mob官方申请的appkey"
appSecret "替换为mob官方申请的appkey对应的appSecret"
gui false // 使用自定义的gui界面
ShareSDK {
//平台配置信息
devInfo {
SinaWeibo { // 新浪微博
appKey "23s050667"
appSecret "a45019672f15f954esdfsef41d4411065"
callbackUri "http://xxx.com/home/Index/indexx.html"
shareByAppClient true // 这里的true为从微博客户端进行启动,而不是从网页版的微博启动
}
Wechat { // 微信好友分享
appId "wx1f84cbafdsad7d477b"
appSecret "f1839f6a28bb0f4xxxsa38150dd9a524"
}
WechatMoments { // 微信朋友圈分享
appId "wx1f84cbafdsad7d477b"
appSecret "f1839f6a28bb0sdf0dd9a524"
}
QQ { // QQ分享
appId "110aafddsa024"
appKey "269afdas1zTcvZ"
}
QZone { // QQ空间分享
appId "110aafddsa024"
appKey "269afdas1zTcvZ"
}
}
}
}
(五):以上的注释都很清楚,比如你只注册了个微信分享,而没有注册微信朋友圈分享的话,在代码中是导入不了微信朋友圈分享的包。
差不多就是上图这样子的。
(六):清单文件中进行配置:
AndroidManifest.xml:
// 微信配置
// QQ配置
(七):WXEntryActivity.java:
public class WXEntryActivity extends WechatHandlerActivity {
/**
* 处理微信发出的向第三方应用请求app message
*
* 在微信客户端中的聊天页面有“添加工具”,可以将本应用的图标添加到其中
* 此后点击图标,下面的代码会被执行。Demo仅仅只是打开自己而已,但你可
* 做点其他的事情,包括根本不打开任何页面
*/
public void onGetMessageFromWXReq(WXMediaMessage msg) {
if (msg != null) {
Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(getPackageName());
startActivity(iLaunchMyself);
}
}
/**
* 处理微信向第三方应用发起的消息
*
* 此处用来接收从微信发送过来的消息,比方说本demo在wechatpage里面分享
* 应用时可以不分享应用文件,而分享一段应用的自定义信息。接受方的微信
* 客户端会通过这个方法,将这个信息发送回接收方手机上的本demo中,当作
* 回调。
*
* 本Demo只是将信息展示出来,但你可做点其他的事情,而不仅仅只是Toast
*/
public void onShowMessageFromWXReq(WXMediaMessage msg) {
if (msg != null && msg.mediaObject != null
&& (msg.mediaObject instanceof WXAppExtendObject)) {
WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject;
Toast.makeText(this, obj.extInfo, Toast.LENGTH_SHORT).show();
}
}
}
(九):来一张效果图:
(十一):点击右上角,进行弹框,然后点击icon进行分享。(代码可能有些乱,欢迎指正)
(1):点击按钮进行弹框:
private void showShareDialog(final String shareTitle, final String shareDesc, final String shareImg, final String shareUrl) {
new ShareDialog(mContext, new ShareDialog.onClickback() {
@Override
public void onShare(int resId) {
switch (resId) {
case Constants.SHARE_WECHAT:
ShareUtil.share(mContext, Constants.SHARE_WECHAT, shareTitle, shareDesc, shareImg, shareUrl);
break;
case Constants.SHARE_WECHAT_CIRCLE:
ShareUtil.share(mContext, Constants.SHARE_WECHAT_CIRCLE, shareTitle, shareDesc, shareImg, shareUrl);
break;
case Constants.SHARE_QQ:
ShareUtil.share(mContext, Constants.SHARE_QQ, shareTitle, shareDesc, shareImg, shareUrl);
break;
case Constants.SHARE_QQ_ZONE:
ShareUtil.share(mContext, Constants.SHARE_QQ_ZONE, shareTitle, shareDesc, shareImg, shareUrl);
break;
case Constants.SHARE_SINA_WEIBO:
ShareUtil.share(mContext, Constants.SHARE_SINA_WEIBO, shareTitle, shareDesc, shareImg, shareUrl);
break;
}
}
}, Constants.SHARE_WECHAT, Constants.SHARE_WECHAT_CIRCLE, Constants.SHARE_QQ, Constants.SHARE_QQ_ZONE, Constants.SHARE_SINA_WEIBO).show();
}
(2)ShareDialog弹框类:
public class ShareDialog extends Dialog {
private onClickback callback;
public ShareDialog(Context context, onClickback callback, int... args) {
this(context, R.layout.dialog_share, R.style.DialogTheme, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
this.callback = callback;
for (int i = 0; i < args.length; i++) {
int arg = args[i];
switch (arg) {
case Constants.SHARE_REPORT:
findViewById(R.id.ll_report).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_FEEDBACK:
findViewById(R.id.ll_feedback).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_DISSMISS_AUTHOR:
findViewById(R.id.ll_author).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_DISSMISS_ARTICLE:
findViewById(R.id.ll_article).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_WECHAT:
findViewById(R.id.ll_share_wechat).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_WECHAT_CIRCLE:
findViewById(R.id.ll_share_wechat_circle).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_SINA_WEIBO:
findViewById(R.id.ll_share_sina_weibo).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_URL:
findViewById(R.id.ll_share_url).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_QQ:
findViewById(R.id.ll_share_qq).setVisibility(View.VISIBLE);
break;
case Constants.SHARE_QQ_ZONE:
findViewById(R.id.ll_share_qq_zone).setVisibility(View.VISIBLE);
break;
}
}
}
public ShareDialog(final Context context, int layout, int style, int width,
int height) {
super(context, style);
setContentView(layout);
setCanceledOnTouchOutside(true);
// 设置属性值
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = width;
lp.height = height;
getWindow().setAttributes(lp);
setListener();
}
// 设置点击事件
private void setListener() {
findViewById(R.id.iv_feedback).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_FEEDBACK);
dismiss();
}
});
findViewById(R.id.tv_cancle).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
dismiss();
}
});
findViewById(R.id.ll_report).setOnClickListener(
new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_REPORT);
dismiss();
}
});
findViewById(R.id.ll_author).setOnClickListener(
new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_DISSMISS_AUTHOR);
dismiss();
}
});
findViewById(R.id.ll_article).setOnClickListener(
new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onShare(Constants.SHARE_DISSMISS_ARTICLE);
dismiss();
}
});
findViewById(R.id.ll_share_wechat).setOnClickListener(
new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_WECHAT);
dismiss();
}
});
findViewById(R.id.ll_share_wechat_circle).setOnClickListener(
new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_WECHAT_CIRCLE);
dismiss();
}
});
findViewById(R.id.ll_share_url).setOnClickListener(
new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_URL);
dismiss();
}
});
findViewById(R.id.ll_share_sina_weibo).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_SINA_WEIBO);
dismiss();
}
});
findViewById(R.id.ll_share_qq).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_QQ);
dismiss();
}
});
findViewById(R.id.ll_share_qq_zone).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
callback.onShare(Constants.SHARE_QQ_ZONE);
dismiss();
}
});
}
@Override
public void show() {
super.show();
//设置dialog显示动画
getWindow().setWindowAnimations(R.style.main_menu_animStyle);
// 设置显示位置为底部
getWindow().setGravity(Gravity.BOTTOM);
}
public interface onClickback {
void onShare(int resId);
}
}
(3)ShareUtil的工具类:
public class ShareUtil {
private static final String TAG = "ShareUtil";
/**
* @param context 上下文
* @param shareType 朋友圈/微信/QQ
* @param shareTitle
* @param shareText
* @param shareImageUrl
* @param shareUrl
*/
public static void share(Context context, int shareType, String shareTitle, String shareText, String shareImageUrl, String shareUrl) {
Log.e(TAG, "share: " + shareTitle + " shareDes " + shareText + " shareImg " + shareImageUrl + " shareUrl " + shareUrl);
MyShareListener listener = new MyShareListener();
Platform.ShareParams sharePlatform = new Platform.ShareParams();
Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_app);
Platform platform;
if (shareType == Constants.SHARE_WECHAT) {
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
setSharePlatform(sharePlatform, shareTitle, shareText, shareImageUrl, logo, shareUrl);
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
sharePlatform.setTitleUrl(shareUrl);
}
platform = ShareSDK.getPlatform(Wechat.NAME);
if (!platform.isClientValid()) {
GDApplication.getInstance().showToast("未安装微信");
return;
}
platform.setPlatformActionListener(listener);
platform.share(sharePlatform);
} else if (shareType == Constants.SHARE_WECHAT_CIRCLE) {
platform = ShareSDK.getPlatform(WechatMoments.NAME);
if (!platform.isClientValid()) {
GDApplication.getInstance().showToast("未安装微信");
return;
}
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
setSharePlatform(sharePlatform, shareTitle, null, shareImageUrl, logo,shareUrl);
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
sharePlatform.setTitleUrl(shareUrl);
}
platform.setPlatformActionListener(listener);
platform.share(sharePlatform);
}
/**
* 如果是QQ分享
*/
else if (shareType == Constants.SHARE_QQ) {
setSharePlatform(sharePlatform, shareTitle, shareText, shareImageUrl, logo,shareUrl);
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
sharePlatform.setTitleUrl(shareUrl);
}
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
platform = ShareSDK.getPlatform(QQ.NAME);
if (!platform.isClientValid()) {
GDApplication.getInstance().showToast("未安装QQ");
return;
}
platform.setPlatformActionListener(listener);
platform.share(sharePlatform);
} else if (shareType == Constants.SHARE_QQ_ZONE) {
setSharePlatform(sharePlatform, shareTitle, shareText, shareImageUrl, logo,shareUrl);
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
sharePlatform.setTitleUrl(shareUrl);
}
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
platform = ShareSDK.getPlatform(QZone.NAME);
if (!platform.isClientValid()) {
GDApplication.getInstance().showToast("未安装QQ");
return;
}
platform.setPlatformActionListener(listener);
platform.share(sharePlatform);
} else if (shareType == Constants.SHARE_SINA_WEIBO) {
setSharePlatform(sharePlatform, shareTitle, shareText, shareImageUrl, logo,shareUrl );
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
sharePlatform.setTitleUrl(shareUrl);
}
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
platform = ShareSDK.getPlatform(SinaWeibo.NAME);
if (!platform.isClientValid()) {
GDApplication.getInstance().showToast("未安装微博");
return;
}
platform.setPlatformActionListener(listener);
platform.share(sharePlatform);
}
}
private static void setSharePlatform(Platform.ShareParams sharePlatform, String shareTitle, String shareText, String shareImageUrl, Bitmap logo, String url) {
sharePlatform.setShareType(Platform.SHARE_WEBPAGE);
sharePlatform.setTitle(shareTitle);
if (shareText != null && !shareText.equalsIgnoreCase("")) {
sharePlatform.setText(shareText);
}
if (shareImageUrl != null && !shareImageUrl.equalsIgnoreCase("")) {
sharePlatform.setImageUrl(shareImageUrl);
} else {
sharePlatform.setImageData(logo);
}
sharePlatform.setUrl(url);
}
static class MyShareListener implements PlatformActionListener {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
Log.e(TAG, "onComplete: ");
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.e(TAG, "onError: " + i + " throwable " + throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
Log.e(TAG, "onCancel: ");
}
}
}
(4)从底部往上弹出的动画:
<style name="main_menu_animStyle">
- "android:windowEnterAnimation"
>@anim/dialog_in_anim
- "android:windowExitAnimation">@anim/dialog_out_anim
style>
(5)dialog_in_anim.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="1000"
android:toXDelta="0"
android:toYDelta="0" />
set>
(6)dialog_out_anim.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="1000" />
set>
(7)最后是分享的面板布局 dialog_share.xml:
就这样,一个简单的mob sharesdk自定义分享功能就做好了,注意一定要参数传入正确。
关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。
更多资源请访问:
超详细图文搭建个人免费博客
关注「蛇崽网盘教程资源」公众号 ,在微信后台回复「领取资源」,获取IT资源200G干货大全。
在微信后台回复「130个小程序」,即可免费领取享有导入就能跑的微信小程序