如何绕过微信sdk审核进行分享?
2017-07-03 本文已影响227人
ROOM先生
一.概述:
1.最近项目需求中要求一个 反人类的功能,何来反人来.听我慢慢讲.
需求(1.点击分享按钮,吧相关的分享内容分享到微信.(看到这 ,你会认为 这有什么难得,简单的跟屎似得.但是真正的却不是这样的)我又问了需求,原来是分享到微信的内容来源 不是自己的APP,而是 QQ,百度,或者其他平台.)
乍一听 ,感觉一脸懵逼,腾讯的sdk有这个功能吗?
分享内容.png
分享成功后.png
需求如上图:点击返回优酷视频 则返回自己的APP.(而不是返回优酷视频)
看到这个功能,感觉自己撸了这么多年 代码,真的白搭了.
这样的功能怎么办,硬着头皮也得上啊.Google查资料,连个屎都没有,心中一万只小草 尼?? 飘过.不知道谁想到的反人类需求.
不过这个demo的内容不是单单的针对这个功能的,所以用起来代码比较复杂.
我分析了他的代码.
ShareFragmentPresenter.Java.
package com.lpp.onshare.presenter;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.lpp.onshare.base.BasePresenter;
import com.lpp.onshare.utils.LogUtil;
import com.lpp.onshare.utils.ShareUtils;
import com.lpp.onshare.utils.StringUtils;
import java.lang.ref.WeakReference;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* Created by 51425 on 2017/6/12.
*/
public class ShareFragmentPresenter extends BasePresenter {
// private ShareFragment mShareFragment;
// private IShareFragmentView mIShareFragmentView;
private String shareAppId;
private String shareAppPackageName;
/**
* 通过intent 分享内容给微信好友
* @param content 分享描述
*/
public void throughIntentShareWXFriends(String content,int type){
// if (!AppUtils.checkApkExist(mShareFragment.getmActivity(), "com.tencent.mm")) {
// Toast.makeText(App.getContext(), "亲,你还没安装微信", Toast.LENGTH_SHORT).show();
// return;
// }
if (content == null || "".equals(content)){
return;
}
if (type == 0){
ShareUtils.throughIntentShareWXdesc(content);
}else if (type == 1){
}
}
/**
* 通过sdk 进行分享 这里只是提供一种写法,具体项目中怎么写还得看自己的情况,而且我rxjava也不怎么会2333
* @param shareTiele 分享的标题
* @param shareContent 分享的内容
* @param shareImage 分享的图片
* @param jumpUrl 分享出去跳转的链接
* @param type 分享到哪里
*/
public void throughSdkShareWXFriends(final Activity context, final String shareTiele, final String shareContent, final String shareImage, final String jumpUrl, final int type){
Observable.just(ShareUtils.shareWXReadyRx(shareImage))
.filter(new Func1<String[], Boolean>() {
@Override
public Boolean call(String[] strings) {
if (strings == null) {
LogUtil.e("没有任何可以分享的平台");
//这里根据需求来修改,我们是如果没有任何可以而分享的平台就走intent 方式进行分享,utils中也有,这里就不细写了
return false;
} else {
shareAppId = strings[0];
shareAppPackageName = strings[1];
LogUtil.e("分享的appId:::" + shareAppId + "////" + shareAppPackageName);
return true;
}
}
})
.map(new Func1<String[], Bitmap>() {
@Override
public Bitmap call(String[] strings) {
String sharImage = strings[2];
if (StringUtils.isEmpty(shareImage)){
return BitmapFactory.decodeResource(context.getResources(),0);
}
return ShareUtils.getHttpBitmap(sharImage);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Bitmap>() {
@Override
public void onCompleted() {
// if (shareLoading != null) {
//// shareLoading.dismissDialog(context);
// }
}
@Override
public void onError(Throwable e) {
// if (shareLoading != null) {
//// shareLoading.dismissDialog(context);
// }
LogUtil.e("error___");
//如果在分享的过程中出现错误也应该走intent 方式
LogUtil.e(android.util.Log.getStackTraceString(e));
}
@Override
public void onNext(Bitmap bitmap) {
if (bitmap!=null){
ShareUtils.shareWXRX(new WeakReference<Activity>(context), shareAppId, shareAppPackageName, shareTiele
, shareContent, jumpUrl, type, bitmap
);
}
}
});
}
}
ShareUtils.java
package com.lpp.onshare.utils;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import com.lpp.onshare.App;
import com.lpp.onshare.constant.Constant;
import com.lpp.onshare.interfaces.GetResultListener;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.net.URL;
/**
* 作者: wr
* 时间: 2017/4/19 18:27
* 说明: 目前包含intent 分享,uc/qq浏览器分享,修改微信sdk分享,如果以后有新的办法我会继续更新
*/
public class ShareUtils {
//------------------------------------------------------------华丽的分割线------------------------------------------------------------
//通过intent 分享
/**
* 通过intent 方式分享到微信好友(内容)
* 这里需要说明下,经过百度查找加测试发现,如果单纯的通过intent分享内容给微信好友
* 1.分享文字
* 2.分享图片
* 3.两者不能同时分享(如果可以同时的话,麻烦告诉我下谢谢了)
*/
public static void throughIntentShareWXdesc( String share_word) {
try{
Intent intentFriend = new Intent();
ComponentName compFriend = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
intentFriend.setComponent(compFriend);
intentFriend.setAction(Intent.ACTION_SEND);
intentFriend.setType("image/*");
intentFriend.putExtra(Intent.EXTRA_TEXT, share_word);
intentFriend.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intentFriend);
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
/**
* 通过intent 方式分享内容到微信好友
* @param imageUri
*/
public static void throughIntentShareWXImage( String imageUri) {
try{
Intent intentFriend = new Intent();
ComponentName compFriend = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
intentFriend.setComponent(compFriend);
intentFriend.setAction(Intent.ACTION_SEND);
intentFriend.setType("image/*");
intentFriend.putExtra(Intent.EXTRA_STREAM, imageUri);
intentFriend.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intentFriend);
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
/**
* 通过intent方式分享内容到微信朋友圈(朋友圈可以是图片加文字一起分享)
*
* @param shareWord
* @param fileUri
*/
public static void throughIntentShareWXCircle(String shareWord, Uri fileUri) {
if (!AppUtils.checkApkExist(App.getContext(), "com.tencent.mm")) {
Toast.makeText(App.getContext(), "亲,你还没安装微信", Toast.LENGTH_SHORT).show();
return;
}
try{
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
intent.setComponent(comp);
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("Kdescription", shareWord);
intent.putExtra(Intent.EXTRA_STREAM,fileUri);
App.getContext().startActivity(intent);
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
/**
* 通过intent分享到QQ空间 ,这里需要先安装qq空间才能分享,注意:不是qq是qq空间
* @param desc
* @param fileUri
*/
public static void throughIntentShareQQZONE(String desc, String fileUri){
try{
if (fileUri!=null){
Intent intentQZ = new Intent();
ComponentName componentFirendQZ = new ComponentName("com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
intentQZ.setComponent(componentFirendQZ);
intentQZ.setAction(Intent.ACTION_SEND);
intentQZ.setType("image/*");
intentQZ.putExtra(Intent.EXTRA_TEXT, desc);
intentQZ.putExtra(Intent.EXTRA_STREAM, fileUri);
intentQZ.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intentQZ);
}
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
/**
* 通过intent 分享内容到QQ
* @param desc
*/
public static void throughIntentShareQQDesc(String desc){
try{
Intent intent = new Intent();
ComponentName componentFirend = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
intent.setComponent(componentFirend);
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/*");
intent.putExtra(Intent.EXTRA_TEXT,desc);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intent);
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
/**
* 通过intent 分享内容到QQ
* @param fileUri
*/
public static void throughIntentShareQQImage(String fileUri){
try{
if (fileUri != null) {
Intent intent = new Intent();
ComponentName componentFirend = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
intent.setComponent(componentFirend);
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,fileUri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intent);
}
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
}
}
//------------------------------------------------------------华丽的分割线------------------------------------------------------------
/**
* 通过qq浏览器分享,具体的分享内容都是后端那里写的
* @param ucShareUrl 这个url 是跳转到后端写的一个界面,从这个界面再分享到微信或qq
* 当通过这种方式分享到微信好友时,点击返回是返回到浏览器而不是自己的应用
* 其实分享的内容什么的都是后端写好的android只是打开浏览器,并跳转到指定的一个页面
* 如果想更好的体验,就需要在这个界面加上个按钮通过点击这个按钮再返回自己的应用
* 这时就需要定义个scheme
* 目前这种方式只能分享到 qq好友,微信好友,微信朋友圈
* 这里只是简单举个例子,不建议使用
* 这种方式我在项目中已经弃用了...
*/
public static void throughQQBShareWxCircle(String ucShareUrl){
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri content_url = Uri.parse(ucShareUrl + "&type=weixinFriend");
intent.setData(content_url);
intent.setClassName(Constant.WEIXINAPPPACKAGEQQBROWSER, "com.tencent.mtt.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.getContext().startActivity(intent);
}
//------------------------------------------------------------华丽的分割线------------------------------------------------------------
/** 通过回调来实现
* @param AppId 来源id
* @param packageName 来源包名
* @param shareTitle 分享标题
* @param shareContent 分享内容
* @param shareUrl 分享链接
* @param type 分享方式 0 好友 1 朋友圈 2 收藏
* @param shareBitmap 分享图片
*/
public static void shareWX(WeakReference<Activity> weakReference, String AppId, String packageName, String shareTitle, String shareContent, String shareUrl, int type, Bitmap shareBitmap, GetResultListener onShareLitener) {
LogUtil.e("shareWX_______");
Bitmap localBitmap2 = Bitmap.createScaledBitmap(shareBitmap, 150, 150, true);
if (shareBitmap!=null){
shareBitmap.recycle();
shareBitmap=null;
}
//通过原始的微信sdk来组装参数
WXWebpageObject localWXWebpageObject = new WXWebpageObject();
localWXWebpageObject.webpageUrl = shareUrl;
WXMediaMessage localWXMediaMessage = new WXMediaMessage(localWXWebpageObject);
localWXMediaMessage.title = shareTitle;
localWXMediaMessage.description = shareContent;
localWXMediaMessage.thumbData = (bmpToByteArray(localBitmap2, true));
SendMessageToWX.Req localReq = new SendMessageToWX.Req();
localReq.transaction = System.currentTimeMillis() + "";
localReq.message = localWXMediaMessage;
localReq.scene = type;
//在分享的时候不调用sdk中原有的分享代码,改调用自己的,这里需要注意不要使用新的jar包,里面有的方法已经取消了,就用项目里的
WxShare.sendReq(weakReference, onShareLitener, localReq, AppId, packageName);
}
/**
* 做分享前的准备,判断当前有哪个应用能进行分享 (使用回调方式)
* @param weakReference
* @param shareTitle
* @param share_word
* @param shareUrl
* @param type
* @param bitmap
*/
public static void shareWXReady(WeakReference<Activity> weakReference, String shareTitle, String share_word, String shareUrl, int type, Bitmap bitmap, GetResultListener onShareLitener) {
try{
if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEQQ)){
LogUtil.e("安装了QQ");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYQQ,Constant.WEIXINAPPPACKAGEQQ,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener
);
}else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEUC)){
LogUtil.e("安装了uc");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYUC,Constant.WEIXINAPPPACKAGEUC,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener);
}else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEQQBROWSER)){
LogUtil.e("安装了qqBrowser");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYQQBROWSER,Constant.WEIXINAPPPACKAGEQQBROWSER,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener);
}else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGENEWSTODAY)){
LogUtil.e("安装了今日头条");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYNEWSTODAY,Constant.WEIXINAPPPACKAGENEWSTODAY,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener);
}else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEBAIDU)){
LogUtil.e("安装了百度");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYBAIDU,Constant.WEIXINAPPPACKAGEBAIDU,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener);
}else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGESINA)){
LogUtil.e("安装了sina");
ShareUtils.shareWX(weakReference,Constant.WEIXINAPPKEYSINA,Constant.WEIXINAPPPACKAGESINA,shareTitle
,share_word,shareUrl,type,bitmap,onShareLitener);
}else{
LogUtil.e("没有其他的");
// onShareLitener.onError();
return;
}
}catch (Exception e){
LogUtil.e(Log.getStackTraceString(e));
// onShareLitener.onError();
}
}
/**
* 通过rxjava重构分享部分的代码,这里是简写,在真实项目中,可能根据需求的不同要嵌套好几层回调,所以改成rxjava来写
* @param weakReference
* @param AppId
* @param packageName
* @param shareTitle
* @param shareContent
* @param shareUrl
* @param type
* @param shareBitmap
*/
public static void shareWXRX(WeakReference<Activity> weakReference, String AppId, String packageName, String shareTitle, String shareContent, String shareUrl, int type, Bitmap shareBitmap) {
Bitmap localBitmap2 = Bitmap.createScaledBitmap(shareBitmap, 150, 150, true);
if (shareBitmap != null) {
shareBitmap.recycle();
shareBitmap = null;
}
//拼接参数还是用原生的sdk来弄
WXWebpageObject localWXWebpageObject = new WXWebpageObject();
localWXWebpageObject.webpageUrl = shareUrl;
WXMediaMessage localWXMediaMessage = new WXMediaMessage(localWXWebpageObject);
localWXMediaMessage.title = shareTitle;
localWXMediaMessage.description = shareContent;
localWXMediaMessage.thumbData = (bmpToByteArray(localBitmap2, true));
SendMessageToWX.Req localReq = new SendMessageToWX.Req();
localReq.transaction = System.currentTimeMillis() + "";
localReq.message = localWXMediaMessage;
localReq.scene = type;
//在分享的时候不调用sdk中原有的分享代码,改调用自己的,这里需要注意不要使用新的jar包,里面有的方法已经取消了,就用我这项目里的
WxShare.sendReq(weakReference, localReq, AppId, packageName);
}
/**
* 根据手机上安装的软件返回具体的包名和AppID,这里面其实能做很多处理,我这只是安顺序写下来了,一般qq就返回去了。。。
* @param shareImage
* @return
*/
public static String[] shareWXReadyRx(String shareImage) {
String[] strings = new String[3];
if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEQQ)) {
LogUtil.e("安装了QQ");
strings[0] = Constant.WEIXINAPPKEYQQ;
strings[1] = Constant.WEIXINAPPPACKAGEQQ;
strings[2] = shareImage;
return strings;
} else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEUC)) {
LogUtil.e("安装了uc");
strings[0] = Constant.WEIXINAPPKEYUC;
strings[1] = Constant.WEIXINAPPPACKAGEUC;
strings[2] = shareImage;
return strings;
} else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEQQBROWSER)) {
strings[0] = Constant.WEIXINAPPKEYQQBROWSER;
strings[1] = Constant.WEIXINAPPPACKAGEQQBROWSER;
strings[2] = shareImage;
LogUtil.e("安装了qqBrowser");
return strings;
} else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGENEWSTODAY)) {
strings[0] = Constant.WEIXINAPPKEYNEWSTODAY;
strings[1] = Constant.WEIXINAPPPACKAGENEWSTODAY;
strings[2] = shareImage;
LogUtil.e("安装了今日头条");
return strings;
} else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGEBAIDU)) {
strings[0] = Constant.WEIXINAPPKEYBAIDU;
strings[1] = Constant.WEIXINAPPPACKAGEBAIDU;
strings[2] = shareImage;
LogUtil.e("安装了百度");
return strings;
} else if (AppUtils.checkApkExist(Constant.WEIXINAPPPACKAGESINA)) {
strings[0] = Constant.WEIXINAPPKEYSINA;
strings[1] = Constant.WEIXINAPPPACKAGESINA;
strings[2] = shareImage;
LogUtil.e("安装了sina");
return strings;
}
return null;
}
/**
* @param shareImage
* @return
*/
public static Bitmap getHttpBitmap(String shareImage) {
URL pictureUrl = null;
InputStream in = null;
Bitmap bitmap = null;
try {
pictureUrl = new URL(shareImage);
in = pictureUrl.openStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inSampleSize = 2;
SoftReference<Bitmap> bitmapSoftReference = new SoftReference<>(BitmapFactory.decodeStream(in,null,options));
bitmap = bitmapSoftReference.get();
} catch (Exception e) {
LogUtil.e(Log.getStackTraceString(e));
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
LogUtil.e(Log.getStackTraceString(e));
}
}
return bitmap;
}
/**
* 跳转官方安装网址
*/
public static void toInstallWebView(String url) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(Uri.parse(url));
App.getContext().startActivity(intent);
}
public static String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
LogUtil.e("result___长度" + result.length);
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
以上是该功能的核心代码.需要demo的可以添加我的微信公众号(回复:微信分享)获取下载链接:
图片.png