package com.ansunxan.z1.wxapi; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import com.tencent.mm.opensdk.modelbase.BaseReq; import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import com.tencent.mm.opensdk.openapi.WXAPIFactory; import com.yonsz.z1.R; import com.yonsz.z1.database.entity.entitya2.WxLoginEvent; import java.io.ByteArrayOutputStream; import de.greenrobot.event.EventBus; import static com.yonsz.z1.difference.DifferConstans.WX_APP_ID; public class WXEntryActivity extends Activity implements IWXAPIEventHandler { private IWXAPI iwxapi; public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wxentry); iwxapi = WXAPIFactory.createWXAPI(this, WX_APP_ID, false); iwxapi.handleIntent(getIntent(), this); iwxapi.registerApp(WX_APP_ID); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); iwxapi.handleIntent(intent, this); } @Override public void onReq(BaseReq baseReq) { Log.i("BaseReq", "" + baseReq.openId + "---" + baseReq.transaction); } @Override public void onResp(BaseResp resp) { Log.i("BaseReq", "" + resp.openId + "---" + resp.transaction + "===" + resp.errCode + "==" + resp.errStr); String result; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = getString(R.string.Share_success); if (resp instanceof SendAuth.Resp) { String code = ((SendAuth.Resp) resp).code; Log.i("BaseReq", "LoginActivity onResp()" + code); EventBus.getDefault().post( new WxLoginEvent(code)); } else { } break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "取消分享"; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "分享被拒绝"; break; default: result = "发送返回"; break; } finish(); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } enum SHARE_TYPE {Type_WXSceneSession, Type_WXSceneTimeline} }