1.集成微信sdk
首先在build.gradle文件中,添加如下依赖即可:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
或者:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
然后添加必要的权限支持:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
uses-permission android:name="android.permission.READ_PHONE_STATE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
2.在Activity中
private WXShare wxShare;
private FXDialog dialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_layout);
wxShare = new WXShare(getActivity());
wxShare.setListener(new OnResponseListener() {
@Override
public void onSuccess() {
// 分享成功
ToastUtil.ToastMessag("分享成功");
}
@Override
public void onCancel() {
// 分享取消(作废)
}
@Override
public void onFail(String message) {
// 分享失败(作废)
}
});
}
@Override
public void onStart() {
super.onStart();
wxShare.register();
}
@Override
public void onDestroy() {
super.onDestroy();
wxShare.unregister();
}
/**
* 获取Layout截图
*
* @return 所需区域的截图
*/
private Bitmap getBitmap(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(); //启用DrawingCache并创建位图
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
view.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
return bitmap;
}
//使用
dialog = new FXDialog(getActivity(), R.style.recharge_pay_dialog, new OnMultiClickListener() {
@Override
public void OnMoreClick(View v) {
switch (v.getId()){
case R.id.re_pengyou://朋友
if( WXShare.isWeChatAppInstalled(getActivity())){
//wxShare.shareImg(getBitmap(buju), 0);
wxShare.shareImg(getBitmap(buju), 0);//图片,0是朋友1是朋友圈2是收藏
} else {
ToastUtil.ToastMessagsb("未安装微信");
}
dialog.dismiss();
break;
case R.id.re_pengyouquan://朋友圈
if( WXShare.isWeChatAppInstalled(getActivity())){
//wxShare.shareImg(getBitmap(buju), 1);
wxShare.shareImg(getBitmap(buju), 1);//图片,0是朋友1是朋友圈2是收藏
} else {
ToastUtil.ToastMessagsb("未安装微信");
}
dialog.dismiss();
break;
}
}
});
dialog.show();
在styles中添加
<!-- 支付方式Dialog 样式 -->
<style name="recharge_pay_dialog" parent="@android:style/Theme.Dialog">
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<!-- 浮于Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- Dialog以外的区域模糊效果 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- Dialog进入及退出动画 -->
<item name="android:windowAnimationStyle">@style/racharge_dialog_animation</item>
</style>
<style name="racharge_dialog_animation" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
在res下新建anim文件夹,新增打开与关闭动画
dialog_enter
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:duration="300"/>
</set>
dialog_exit
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:toYDelta="100%p" android:duration="400"/>
</set>
FXDialog类
/**
* 功 能: 分享
*/
public class FXDialog extends Dialog {
private RelativeLayout re_pengyou,re_pengyouquan;
private OnMultiClickListener onClickListener;
private Context context;
/**
* @param context
* @param themeResId
*/
public FXDialog(Context context , int themeResId, OnMultiClickListener onClickListener) {
super(context, themeResId);
this.context = context;
this.onClickListener = onClickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_fx);
re_pengyou = findViewById(R.id.re_pengyou);
re_pengyouquan = findViewById(R.id.re_pengyouquan);
Window dialogWindow = getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = AbsListView.LayoutParams.MATCH_PARENT;
lp.y = 0;//设置Dialog距离底部的距离
dialogWindow.setAttributes(lp);
re_pengyou.setOnClickListener(onClickListener);
re_pengyouquan.setOnClickListener(onClickListener);
}
}
dialog_fx
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/re_pengyou"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="180dp"
>
<RelativeLayout
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/pengyouimg"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@mipmap/weixin"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pengyouimg"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="朋友"
android:textColor="@color/textcolor"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/re_pengyouquan"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="180dp"
android:orientation="vertical">
<RelativeLayout
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/pengyouquanimg"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@mipmap/weixinpengyouquan"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pengyouquanimg"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="朋友圈"
android:textColor="@color/textcolor"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
最后:返回状态
/**
* 微信登录,分享返回
*/
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
public static String WX_APP_ID = Wxutil.APP_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, WX_APP_ID, false);
api.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
finish();
}
// 微信发送请求到第三方应用时,会回调到该方法
@Override
public void onReq(BaseReq req) {
finish();
}
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
@Override
public void onResp(BaseResp resp) {
// resp.getType(); 1:第三方授权(登录等), 2:分享
int dlfxType = resp.getType();
//分享接口调用后,将不再返回分享结果事件(全部为成功)
if (dlfxType == 2) {//分享的回调
Intent intent = new Intent(WXShare.ACTION_SHARE_RESPONSE);
intent.putExtra(WXShare.EXTRA_RESULT, new WXShare.Response(resp));
sendBroadcast(intent);
finish();
} else {//微信登录回调
switch (resp.errCode) {
//ERR_OK = 0(用户同意) ERR_AUTH_DENIED = -4(用户拒绝授权) ERR_USER_CANCEL = -2(用户取消)
case BaseResp.ErrCode.ERR_OK://成功
finish();
break;
case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
ToastUtil.ToastMessagsb("取消了微信登录");
finish();
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
ToastUtil.ToastMessagsb("拒绝了微信登录");
finish();
break;
default:
ToastUtil.ToastMessagsb("微信登录失败");
finish();
break;
}
}
}
}