package com.nababy.shares;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
/**
* Created by ZKTH
* Time : 2018/12/28.10:04
* data :
*/
public class WXShare {
//这个APP_ID是我个人的,可以供大家使用
public static final String APP_ID = "wx3a68bd019f4f3ee7";
public static final String ACTION_SHARE_RESPONSE = "action_wx_share_response";
public static final String EXTRA_RESULT = "result";
private final Context context;
private final IWXAPI api;
private OnResponseListener listener;
private ResponseReceiver receiver;
public WXShare(Context context) {
api = WXAPIFactory.createWXAPI(context, APP_ID);
this.context = context;
}
public WXShare register() {
// 微信分享
api.registerApp(APP_ID);
receiver = new ResponseReceiver();
IntentFilter filter = new IntentFilter(ACTION_SHARE_RESPONSE);
context.registerReceiver(receiver, filter);
return this;
}
public void unregister() {
try {
api.unregisterApp();
context.unregisterReceiver(receiver);
} catch (Exception e) {
e.printStackTrace();
}
}
public WXShare share(String text) {
WXTextObject textObj = new WXTextObject();
textObj.text = text;
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
// msg.title = "Will be ignored";
msg.description = text;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
boolean result = api.sendReq(req);
Log.e("text shared: " + result,"");
return this;
}
//flag用来判断是分享到微信好友还是分享到微信朋友圈,
//0代表分享到微信好友,1代表分享到朋友圈
public WXShare shareUrl(int flag,Context context,String url,String title,String descroption){
//初始化一个WXWebpageObject填写url
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = url;
//用WXWebpageObject对象初始化一个WXMediaMessage,天下标题,描述
WXMediaMessage msg = new WXMediaMessage(webpageObject);
msg.title = title;
msg.description = descroption;
//这块需要注意,图片的像素千万不要太大,不然的话会调不起来微信分享,
//我在做的时候和我们这的UIMM说随便给我一张图,她给了我一张1024*1024的图片
//当时也不知道什么原因,后来在我的机智之下换了一张像素小一点的图片好了!
Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
msg.setThumbImage(thumb);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = flag==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;
api.sendReq(req);
return this;
}
public IWXAPI getApi() {
return api;
}
public void setListener(OnResponseListener listener) {
this.listener = listener;
}
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
private class ResponseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Response response = intent.getParcelableExtra(EXTRA_RESULT);
Log.e("type: " + response.getType(),"type: " + response.getType());
Log.e("errCode: " + response.errCode,"errCode: " + response.errCode);
String result;
if (listener != null) {
if (response.errCode == BaseResp.ErrCode.ERR_OK) {
listener.onSuccess();
} else if (response.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
listener.onCancel();
} else {
switch (response.errCode) {
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "发送被拒绝";
break;
case BaseResp.ErrCode.ERR_UNSUPPORT:
result = "不支持错误";
break;
default:
result = "发送返回";
break;
}
listener.onFail(result);
}
}
}
}
public static class Response extends BaseResp implements Parcelable {
public int errCode;
public String errStr;
public String transaction;
public String openId;
private int type;
private boolean checkResult;
public Response(BaseResp baseResp) {
errCode = baseResp.errCode;
errStr = baseResp.errStr;
transaction = baseResp.transaction;
openId = baseResp.openId;
type = baseResp.getType();
checkResult = baseResp.checkArgs();
}
@Override
public int getType() {
return type;
}
@Override
public boolean checkArgs() {
return checkResult;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.errCode);
dest.writeString(this.errStr);
dest.writeString(this.transaction);
dest.writeString(this.openId);
dest.writeInt(this.type);
dest.writeByte(this.checkResult ? (byte) 1 : (byte) 0);
}
protected Response(Parcel in) {
this.errCode = in.readInt();
this.errStr = in.readString();
this.transaction = in.readString();
this.openId = in.readString();
this.type = in.readInt();
this.checkResult = in.readByte() != 0;
}
public static final Creator<Response> CREATOR = new Creator<Response>() {
@Override
public Response createFromParcel(Parcel source) {
return new Response(source);
}
@Override
public Response[] newArray(int size) {
return new Response[size];
}
};
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
share.rar (95个子文件)
share
.gitignore 127B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 236B
local.properties 560B
build.gradle 573B
gradlew.bat 2KB
share.iml 859B
gradlew 5KB
app
.gitignore 8B
build.gradle 1KB
proguard-rules.pro 772B
app.iml 12KB
libs
src
test
java
com
nababy
shares
ExampleUnitTest.java 411B
main
AndroidManifest.xml 1KB
java
com
nababy
shares
AppRegister.java 612B
ShareApplication.java 305B
WXEntryActivity.java 3KB
OnResponseListener.java 317B
MainActivity.java 1KB
WXShare.java 7KB
res
values
styles.xml 370B
colors.xml 214B
strings.xml 77B
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
layout
activity_main.xml 624B
activity_wxentry.xml 217B
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 276B
ic_launcher_round.xml 276B
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable
ic_launcher_background.xml 6KB
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
androidTest
java
com
nababy
shares
ExampleInstrumentedTest.java 761B
release
app-release.apk 1.52MB
output.json 189B
build
.gradle
4.1
fileChanges
last-build.bin 1B
taskHistory
fileSnapshots.bin 3.28MB
taskHistory.bin 110KB
taskHistory.lock 17B
javaCompile
taskJars.bin 26KB
classAnalysis.bin 1.01MB
taskHistory.bin 197KB
jarAnalysis.bin 420KB
javaCompile.lock 17B
fileContent
fileContent.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 20KB
fileHashes.bin 438KB
buildOutputCleanup
cache.properties 51B
built.bin 0B
cache.properties.lock 2B
build
gradle.properties 747B
.idea
libraries
android_arch_lifecycle_common_1_0_0_jar.xml 548B
com_android_support_support_compat_26_1_0.xml 692B
org_hamcrest_hamcrest_integration_1_3_jar.xml 578B
junit_junit_4_12_jar.xml 487B
com_android_support_test_espresso_espresso_core_3_0_2.xml 710B
org_hamcrest_hamcrest_core_1_3_jar.xml 543B
com_android_support_constraint_constraint_layout_1_1_3.xml 501B
com_android_support_support_media_compat_26_1_0.xml 722B
net_sf_kxml_kxml2_2_3_0_jar.xml 285B
org_hamcrest_hamcrest_library_1_3_jar.xml 557B
com_android_support_support_core_ui_26_1_0.xml 697B
com_google_code_findbugs_jsr305_2_0_1_jar.xml 359B
com_android_support_constraint_constraint_layout_solver_1_1_3_jar.xml 425B
com_qmuiteam_qmui_1_2_0.xml 623B
com_android_support_test_espresso_espresso_idling_resource_3_0_2.xml 765B
javax_inject_javax_inject_1_jar.xml 528B
com_android_support_support_fragment_26_1_0.xml 702B
com_squareup_javawriter_2_1_1_jar.xml 538B
com_android_support_animated_vector_drawable_26_1_0.xml 742B
com_android_support_test_runner_1_0_2.xml 657B
com_android_support_appcompat_v7_26_1_0.xml 682B
com_tencent_mm_opensdk_wechat_sdk_android_without_mta_5_4_0_jar.xml 668B
android_arch_core_common_1_0_0_jar.xml 533B
com_android_support_support_annotations_26_1_0_jar.xml 608B
com_android_support_support_vector_drawable_26_1_0.xml 737B
com_android_support_support_core_utils_26_1_0.xml 712B
com_android_support_test_monitor_1_0_2.xml 662B
com_android_support_support_v4_26_1_0.xml 672B
android_arch_lifecycle_runtime_1_0_0.xml 658B
runConfigurations.xml 564B
workspace.xml 185KB
misc.xml 2KB
modules.xml 347B
gradle.xml 626B
settings.gradle 16B
share.jks 2KB
共 95 条
- 1
资源评论
small小小世界world
- 粉丝: 41
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功