官方:https://open.weixin.qq.com/
一、如何集成
二、回调如何成功
三、借用其他AppId
四、Android11版本拉起微信提示未安装
还是先准备底部弹窗popwindow
public class RewritePopwindow extends PopupWindow {
private View mView;
public RewritePopwindow(Activity context, View.OnClickListener itemsOnClick) {
super(context);
initView(context, itemsOnClick);
}
private void initView(final Activity context, View.OnClickListener itemsOnClick) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = mInflater.inflate(R.layout.popw_share, null);
LinearLayout wxPeople = (LinearLayout) mView.findViewById(R.id.wx_people);
LinearLayout wxFriend = (LinearLayout) mView.findViewById(R.id.wx_friend);
LinearLayout link = (LinearLayout) mView.findViewById(R.id.link);
TextView canaleTv = (TextView) mView.findViewById(R.id.share_cancle);
canaleTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//销毁弹出框
dismiss();
backgroundAlpha(context, 1f);
}
});
//设置按钮监听
wxPeople.setOnClickListener(itemsOnClick);
wxFriend.setOnClickListener(itemsOnClick);
link.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(WindowManager.LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置PopupWindow可触摸
this.setTouchable(true);
//设置非PopupWindow区域是否可触摸
// this.setOutsideTouchable(false);
//设置SelectPicPopupWindow弹出窗体动画效果
// this.setAnimationStyle(R.style.select_anim);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x00000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
backgroundAlpha(context, 0.5f);//0.0-1.0
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
backgroundAlpha(context, 1f);
}
});
}
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(Activity context, float bgAlpha) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.alpha = bgAlpha;
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
context.getWindow().setAttributes(lp);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:alpha="0.2"
android:layout_height="match_parent"
android:layout_above="@+id/ll"/>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:paddingBottom="30dp"
android:id="@+id/share_ll"
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:background="#ffffff"
android:paddingLeft="20dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/wx_people"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@drawable/icon_wx"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="share"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="微信好友"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/wx_friend"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@drawable/icon_friend"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="share"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="朋友圈"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/link"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@drawable/icon_link"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="share"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="复制链接"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/share_ll"
android:background="#000000"/>
<TextView
android:id="@+id/share_cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:layout_below="@+id/line"
android:background="#ffffff"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="取消"/>
</LinearLayout>
</RelativeLayout>
使用
1.开始集成微信相关
//微信
api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
2.做一个分享的管理类,判断权限,是否安装了应用
public class ShareManager {
private final Context mContext;
public ShareManager(Context context) {
mContext = context;
}
@SuppressLint("WrongConstant")
public void startToShare(ShareModel shareModel, ShareResultListener resultListener) {
if (shareModel == null) {
Toast.makeText(mContext, "内容为空,分享失败", Toast.LENGTH_SHORT).show();
return;
}
if (PermissionChecker.checkSelfPermission(mContext, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(mContext, "请到设置->应用->权限中打开SD卡读写权限!", Toast.LENGTH_SHORT).show();
return;
}
if (isThirdAppInstallWithShareType()) {
Toast.makeText(mContext, "应用未安装", Toast.LENGTH_SHORT).show();
return;
}
if (shareModel.getScene() == SendMessageToWX.Req.WXSceneSession) {//微信
WeChatShareUtil.getInstance(mContext).shareToWeChat(shareModel);
} else {
WeChatShareUtil.getInstance(mContext).shareToTimeLine(shareModel);
}
WXBaseEntryActivity.mShareResultListener = resultListener;
}
/**
* 判断手机是否安装了该平台App
*/
public boolean isThirdAppInstallWithShareType() {
return WeChatShareUtil.getInstance(mContext).isInstalled();
}
/**
* 分享结果的listener
*/
public interface ShareResultListener {
void onShareResultBlock(ShareResult iEShareResult, int shareType, String error);
}
}
public enum ShareResult {
/**
* 分享结果
*/
CTShareResultSuccess(1), // 分享成功
CTShareResultCancel(2), // 分享被取消
private int value;
// 构造方法
ShareResult(int eValue) {
value = eValue;
}
public int getValue() {
return value;
}
}
3.调起
public class WeChatShareUtil {
public static final String App_ID = "产品的Id";
private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
private IWXAPI api;
private Context context;
public static WeChatShareUtil weChatShareUtil;
private static final int THUMB_SIZE = 150;
public static WeChatShareUtil getInstance(Context context) {
if (weChatShareUtil == null) {
weChatShareUtil = new WeChatShareUtil();
}
if (weChatShareUtil.api != null) {
weChatShareUtil.api.unregisterApp();
}
weChatShareUtil.context = context;
weChatShareUtil.regToWx();
return weChatShareUtil;
}
public static WeChatShareUtil getInstance() {
return getInstance(null);
}
public void handleIntent(Intent data, IWXAPIEventHandler handler) {
api.handleIntent(data, handler);
}
//注册应用id到微信
private void regToWx() {
api = WXAPIFactory.createWXAPI(this, App_ID, false);
api.registerApp(App_ID);
}
//判断微信
public boolean isInstalled() {
return api.getWXAppSupportAPI() < Build.PAY_SUPPORTED_SDK_INT;
}
//微信
public void shareToWeChat(final ShareModel shareModel) {
new Thread(() -> {
String webpageUrl = shareModel.getWebPageUrl();
String imageUrl = shareModel.getImageUrl();
Bitmap bitmap = shareModel.getBitmap();
boolean isShareWebpage = !TextUtils.isEmpty(webpageUrl) || (bitmap == null && TextUtils.isEmpty(imageUrl));
SendMessageToWX.Req request = new SendMessageToWX.Req();
if (isShareWebpage) {//分享网页
Bitmap thumBitmap = ShareUtil.getShareBitmap(shareModel.getImageUrl(),context);
WXWebpageObject webPage = new WXWebpageObject();
webPage.webpageUrl = shareModel.getWebPageUrl();
WXMediaMessage msg = new WXMediaMessage(webPage);
if (shareModel.getTitle() != null) {
msg.title = shareModel.getTitle();
}
if (shareModel.getDescription() != null) {
msg.description = shareModel.getDescription();
}
if (thumBitmap != null) {
msg.thumbData = bmpToByteArray(thumBitmap, false);
}
request.transaction = String.valueOf(System.currentTimeMillis());
request.message = msg;
Log.d("测试","微信分享网页");
}
.........
//可以自己做一些分享
request.scene = SendMessageToWX.Req.WXSceneSession;
api.sendReq(request);
}).start();
}
//朋友圈
public void shareToTimeLine(final ShareModel shareModel) {
new Thread(new Runnable() {
@Override
public void run() {
String webPageUrl = shareModel.getWebPageUrl();
String imageUrl = shareModel.getImageUrl();
Bitmap bitmap = shareModel.getBitmap();
//根据需求来
boolean isShareWebpage = !TextUtils.isEmpty(webPageUrl) || (bitmap == null && TextUtils.isEmpty(imageUrl));
WXWebpageObject obj = new WXWebpageObject();
obj.webpageUrl = shareModel.getWebPageUrl();
WXMediaMessage media = new WXMediaMessage(obj);
media.title = shareModel.getTitle();
media.description = TextUtils.isEmpty(shareModel.getDescription()) ? "" : shareModel.getDescription();
SendMessageToWX.Req request = new SendMessageToWX.Req();
// Bitmap thumBitmap = ShareUtil.getShareBitmap(shareModel.getImageUrl(),context);
if (isShareWebpage) {
if (bitmap != null) {
media.thumbData = bmpToByteArray(bitmap, false);
}
request.transaction = "webpage";
request.message = media;
Log.d("测试朋友圈","微信分享网页");
}
request.scene = SendMessageToWX.Req.WXSceneTimeline;
api.sendReq(request);
}
}).start();
}
//判断是否支持转发到朋友圈
//微信4.2以上支持,如果需要检查微信版本支持API的情况, 可调用IWXAPI的getWXAppSupportAPI方法,0x21020001及以上支持发送朋友圈
public boolean isSupportWX() {
int wxSdkVersion = api.getWXAppSupportAPI();
return wxSdkVersion >= TIMELINE_SUPPORTED_VERSION;
}
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
int i;
int j;
if (bmp.getHeight() > bmp.getWidth()) {
i = bmp.getWidth();
j = bmp.getWidth();
} else {
i = bmp.getHeight();
j = bmp.getHeight();
}
Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
Canvas localCanvas = new Canvas(localBitmap);
while (true) {
localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0,i, j), null);
if (needRecycle)
bmp.recycle();
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
localBitmap.recycle();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
try {
localByteArrayOutputStream.close();
return arrayOfByte;
} catch (Exception e) {
//F.out(e);
}
i = bmp.getHeight();
j = bmp.getHeight();
}
}
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
调起时先判断
if (weChatShareUtil.isSupportWX()) {
ShareManager shareManager = new ShareManager(MainActivity.this);
shareManager.startToShare(bean, (iEShareResult, shareType, error) -> {
});
}
4.分享的结果
要想成功,一定要和申请的包名相对应
建一样的包名 在建wxapi包
/**
* @author
* 该Activity为微信分享的BaseActivity,已经实现了基础的分享功能,如需定制逻辑在主工程的WXEntryActivity中添加
*/
public class WXBaseEntryActivity extends Activity implements IWXAPIEventHandler {
public static ShareManager.ShareResultListener mShareResultListener;
public static int shareTypeEnum;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// IWXAPI api = WXAPIFactory.createWXAPI(this, WeChatShareUtil.App_ID, false);
IWXAPI api = WXAPIFactory.createWXAPI(this, WeChatShareUtil.App_ID, false);
api.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
WeChatShareUtil.getInstance().handleIntent(intent, this);
}
@Override
public void onResp(BaseResp baseResp) {
Log.e("WXBaseEntryActivity", "onResp");
String result;
switch (baseResp.errCode) {
case BaseResp.ErrCode.ERR_OK:
mShareResultListener.onShareResultBlock(ShareResult.CTShareResultSuccess, shareTypeEnum, "分享成功");
result = "分享成功";
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
mShareResultListener.onShareResultBlock(ShareResult.CTShareResultCancel, shareTypeEnum, "分享取消");
result = null;
break;
default:
result = "分享失败";
break;
}
if (result != null) {
Toast.makeText(this, baseResp.errCode + result, Toast.LENGTH_SHORT).show();
}
finish();
}
}
注意包名和类名
如果你还没有Id,想借用其他产品微信Id,可以改下上下文
api = WXAPIFactory.createWXAPI(new ContextWrapper(context) {
@Override
public String getPackageName() {
return "借用那个产品包名”;
}
}, App_ID, false);
api.registerApp(App_ID);
四、Android10.0及以上版本 微信登录、分享、支付提示微信未安装
在清单文件中配置
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />
官方文档
官方微信开放平台