android 微信分享详情

  1. 微信分享朋友圈和微信好友要进行SDK的下载,libammsdk.jar的jar包,集成到libs当中,集成之后就可以在项目中集成相应代码了。

  2. 分享微信朋友圈和微信好友

(1)要在项目包下创建一个wxapi包名,里面创建一个WXEntryActivity类,并且要实现IWXAPIEventHandler这个接口。实现上面的接口,会复写onReq()和onResp()两个方法。其中onReq()方法,微信发送请求到第三方应用时,会回调到该方法。而onResp()第三方应用发送到微信的请求处理后的响应结果,会回调到该方法。

示例代码:

@Override

public void onResp(BaseResp resp) {

int result = 0;

switch (resp.errCode){

case BaseResp.ErrCode.ERR_OK:

result = “发送成功”;

break;

case BaseResp.ErrCode.ERR_USER_CANCEL:

result = “发送取消”;

break;

case BaseResp.ErrCode.ERR_AUTH_DENIED:

result = “发送被拒绝”;

break;

default:

result = “发送返回”;

break;

}

Toast.makeText(this, result, Toast.LENGTH_LONG).show();

finish();

}

(2)IWXAPI是第三方app和微信通信的openapi接口对象

IWXAPI api = WXAPIFactory.createWXAPI(this,wxb91199337fe56a90, false);

//然后要进行注册(wxb91199337fe56a69==微信开发平台上的AppId);

api.registerApp(wxb91199337fe56a69);

//进行参数的判断

title=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TITLE); description=getIntent().getStringExtra(HDCivilizationConstants.SHARE_DESRIPTION); shareFilePath=getIntent().getStringExtra(HDCivilizationConstants.SHARE_IMG_PATH); webpageUrl=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TARGET_URL);

sceneFlag=getIntent().getBooleanExtra(HDCivilizationConstants.SHARE_SCENEFLAG, false);

shareType=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TYPE);

(3)然后进行具体参数判断和使用

private void share2Wx(){

// 通过WXAPIFactory工厂,获取IWXAPI的实例

//首先进行检测微信的版本

int wxSdkVersion = api.getWXAppSupportAPI();

if (api.isWXAppSupportAPI() && api.isWXAppInstalled()){

//进行支持:看是否分享到朋友圈还是微信的好友

if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_WEBURL)){

//网络类型

WXWebpageObject webpage = new WXWebpageObject();

webpage.webpageUrl =this.webpageUrl;

final WXMediaMessage msg = new WXMediaMessage(webpage);

msg.mediaObject=webpage;

//待定

msg.title = title;

msg.description =description ;

if(shareFilePath!=null && !shareFilePath.equals(“”)){

// if(shareFilePath.startsWith(“http://”)){

// new Thread(){

// @Override

// public void run() {

// try {

// Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());

// Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);

// bmp.recycle();

// msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

// } catch (IOException e) {

// e.printStackTrace();

// }

// }

// }.start();

// }else{

// //本地路径

// System.out.println(“WX FILE PATH:”+shareFilePath);

// Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);

// Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);

// bmp.recycle();

// msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

// }

//本地路径

System.out.println(“WX FILE PATH:”+shareFilePath);

Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);

Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,69, true);

bmp.recycle();

msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

}else{

Bitmap bmp =BitmapFactory.decodeResource(//

UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);

Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 69, true);

bmp.recycle();

msg.thumbData=Util.bmpToByteArray(thumbBmp, true);

}

SendMessageToWX.Req req = new SendMessageToWX.Req();

req.transaction = buildTransaction(“webpage”);

req.message = msg;

req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;

api.sendReq(req);

finish();

}else if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_IMAGE)){

//网络类型

final WXImageObject imgObject = new WXImageObject();

WXMediaMessage msg = new WXMediaMessage(imgObject);

//

if(shareFilePath!=null && !shareFilePath.equals(“”)){

System.out.println(“WX FILE PATH:”+shareFilePath);

// if(shareFilePath.startsWith(“http://”)){

// new Thread(){

// @Override

// public void run() {

// try {

// Bitmap bmp=BitmapFactory.decodeStream(new URL(shareFilePath).openStream());

// Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);

// bmp.recycle();

// imgObject.imagePath=shareFilePath;

// imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);

// imgObject.imageUrl=shareFilePath;

// } catch (IOException e) {

// e.printStackTrace();

// }

// }

// }.start();

// }else{

// //本地路径

// Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);

// Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);

// bmp.recycle();

// imgObject.imagePath=shareFilePath;

// imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);

// imgObject.imageUrl=shareFilePath;

// }

Bitmap bmp = BitmapFactory.decodeFile(shareFilePath);

Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,99,99, true);

bmp.recycle();

imgObject.imagePath=shareFilePath;

imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);

imgObject.imageUrl=shareFilePath;

}else{

Bitmap bmp =BitmapFactory.decodeResource(//

UiUtils.getInstance().getContext().getResources(), R.mipmap.ic_launcher_logo);

Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);

bmp.recycle();

imgObject.imagePath=shareFilePath;

imgObject.imageData = Util.bmpToByteArray(thumbBmp, true);

imgObject.imageUrl=shareFilePath;

}

// if(!FileUtils.getInstance().isExistsFile(MyApplication.shareZinCodePath)){

// try {

// FileUtils.getInstance().writeToDir(getAssets().open(“mine_share_code.png”),MyApplication.shareZinCodePath);

// } catch (IOException e){

// e.printStackTrace();

// }

// }

// Bitmap bmp =BitmapFactory.decodeFile(MyApplication.shareZinCodePath);

// Bitmap thumbBmp=Bitmap.createScaledBitmap(bmp, 99, 99, true);

// imgObject.imagePath=MyApplication.shareZinCodePath;

// imgObject.imageData=Util.bmpToByteArray(thumbBmp, true);

// imgObject.imageUrl=MyApplication.shareZinCodePath;

// bmp.recycle();

msg.thumbData=imgObject.imageData;

//待定

msg.title = “测试app”;

msg.description =“测试app 某某模块” ;

//待定

SendMessageToWX.Req req = new SendMessageToWX.Req();

req.transaction = buildTransaction(“webpage”);

req.message = msg;

req.scene = sceneFlag ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;

api.sendReq(req);

finish();

}

}else{

Toast.makeText(UiUtils.getInstance().getContext(),

“您的微信的版本过低版本,或者未装微信客户端”, Toast.LENGTH_LONG).show();

}

}

以上是基本的使用步骤,可能写的有点乱,下面给大家贴出完成的代码:

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{

private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;

private Button gotoBtn, regBtn, launchBtn, checkBtn;

// IWXAPI 是第三方app和微信通信的openapi接口

private IWXAPI api;

private boolean flag;

private String title;

private String description;

private String shareFilePath;

private String webpageUrl;

private boolean sceneFlag; //判断朋友圈的状态 true时是朋友圈 false微信好友

private String shareType=“”;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//通过WXAPIFactory工厂,获取IWXAPI的实例

api = WXAPIFactory.createWXAPI(this, HDCivilizationConstants.WXAPP_ID, false);

//进行注册

api.registerApp(HDCivilizationConstants.WXAPP_ID);

api.handleIntent(getIntent(), this);

//进行参数的判断

title=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TITLE);

description=getIntent().getStringExtra(HDCivilizationConstants.SHARE_DESRIPTION);

shareFilePath=getIntent().getStringExtra(HDCivilizationConstants.SHARE_IMG_PATH);

webpageUrl=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TARGET_URL);

sceneFlag=getIntent().getBooleanExtra(HDCivilizationConstants.SHARE_SCENEFLAG, false);

shareType=getIntent().getStringExtra(HDCivilizationConstants.SHARE_TYPE);

share2Wx();

}

@Override

protected void onNewIntent(Intent intent){

super.onNewIntent(intent);

setIntent(intent);

api.handleIntent(intent, this);

}

// 微信发送请求到第三方应用时,会回调到该方法

@Override

public void onReq(BaseReq req) {

// Toast.makeText(UiUtils.getContext(), “发送了请求”, Toast.LENGTH_SHORT).show();

}

//第三方应用发送到微信的请求处理后的响应结果,会回调到该方法

@Override

public void onResp(BaseResp resp) {

int result = 0;

switch (resp.errCode){

case BaseResp.ErrCode.ERR_OK:

result = “发送成功”;

break;

case BaseResp.ErrCode.ERR_USER_CANCEL:

result = “发送取消”;

break;

case BaseResp.ErrCode.ERR_AUTH_DENIED:

result = “发送被拒绝”;

break;

default:

result = “发送返回”;

break;

}

Toast.makeText(this, result, Toast.LENGTH_LONG).show();

finish();

}

//新添加的方法

private String buildTransaction(final String type) {

return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();

}

/**

  • @param

*/

private void share2Wx(){

// 通过WXAPIFactory工厂,获取IWXAPI的实例

//首先进行检测微信的版本

int wxSdkVersion = api.getWXAppSupportAPI();

if (api.isWXAppSupportAPI() && api.isWXAppInstalled()){

//进行支持:看是否分享到朋友圈还是微信的好友

if(shareType!=null && shareType.equals(HDCivilizationConstants.SHARE_TYPE_WEBURL)){

//网络类型

WXWebpageObject webpage = new WXWebpageObject();

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助**。

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-hqBGEj1X-1715363934028)]

[外链图片转存中…(img-RBWWwcgX-1715363934030)]

[外链图片转存中…(img-zbX89cmO-1715363934031)]

[外链图片转存中…(img-gjJuK8Mt-1715363934032)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值