Android 微信分享到朋友及朋友圈

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中添加


    

    

在res下新建anim文件夹,新增打开与关闭动画

dialog_enter


    

dialog_exit


    

 

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



    

        

        

        
        

    


    

        

            

            
        

    


最后:返回状态

/**
 * 微信登录,分享返回
 */

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;
            }
        }
    }
}

 

 

 

 

 

你可能感兴趣的:(android)