android微信分享成功不回调
 package com.xcy.wxtest.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xcy.wxtest.R;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{

    private EditText editText;
    private Button button;
    private Button btn_open_wx;
    private IWXAPI apis;
    private static final String APP_ID = "wx8368d8d70626b02e";
    private static final String App_Secret = "a212499f83a99516b45f1fb9c5686e9a";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        apis = WXAPIFactory.createWXAPI(this, APP_ID, false);

        editText = (EditText)findViewById(R.id.editText);
        button = (Button)findViewById(R.id.button);
        btn_open_wx = (Button)findViewById(R.id.btn_open_wx);

        addListeners();
    }

    private void addListeners() {
        System.out.println("=========listener=============");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();

                if(text == null || text.length() == 0 ){
                    Toast.makeText(WXEntryActivity.this, "输入内同不为空", Toast.LENGTH_SHORT).show();
                }

                WXTextObject textobj = new WXTextObject();
                textobj.text = text;

                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = textobj;
                msg.description = text;

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTraction("text");
                req.message = msg;
                req.scene = req.WXSceneTimeline;//WXSceneTimeline 朋友圈 ,Session 表示发送到会话

                apis.sendReq(req);

            }
        });

        btn_open_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(WXEntryActivity.this, "launch result:" + apis.openWXApp(), Toast.LENGTH_SHORT ).show();
            }
        });
    }
    @Override
    public void onReq(BaseReq req) {
        System.out.println("=========onReq=============");
    }

//    @Override
//    public void onResp(BaseResp resp) {
//        System.out.println("=========onResp=============");
//        if(resp.getType() == ConstantsAPI.COMMAND_SENDAUTH){
//            Toast.makeText(WXEntryActivity.this, "code = "+((SendAuth.Resp) resp).errCode, Toast.LENGTH_SHORT).show();
//        }
//        switch(resp.errCode){
//            case BaseResp.ErrCode.ERR_OK:
//                Toast.makeText(WXEntryActivity.this, "分享成功!", Toast.LENGTH_SHORT);
//                break;
//            case BaseResp.ErrCode.ERR_AUTH_DENIED:
//                Toast.makeText(WXEntryActivity.this, "分享被拒绝!", Toast.LENGTH_SHORT);
//                break;
//
//        }
//    }

    @Override
    public void onResp(BaseResp resp) {
        Toast.makeText(this, "openid = " + resp.openId, Toast.LENGTH_SHORT).show();
        System.out.println("================onResp=================");
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            Toast.makeText(this, "code = " + ((SendAuth.Resp) resp).code, Toast.LENGTH_SHORT).show();
        }

        String result = "";

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

    private String buildTraction(final String type){
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}

1个回答

你没有写,api.handleIntent(getIntent(), this); 这句话

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问