WXEntryActivity.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.wsj.Imgdt3;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.widget.Toast;
  8. import com.tencent.mm.opensdk.constants.ConstantsAPI;
  9. import com.tencent.mm.opensdk.modelbase.BaseReq;
  10. import com.tencent.mm.opensdk.modelbase.BaseResp;
  11. import com.tencent.mm.opensdk.modelbiz.SubscribeMessage;
  12. import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
  13. import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessView;
  14. import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview;
  15. import com.tencent.mm.opensdk.modelmsg.SendAuth;
  16. import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
  17. import com.tencent.mm.opensdk.modelmsg.WXAppExtendObject;
  18. import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
  19. import com.tencent.mm.opensdk.openapi.IWXAPI;
  20. import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
  21. import com.tencent.mm.opensdk.openapi.WXAPIFactory;
  22. import java.lang.ref.WeakReference;
  23. public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
  24. private static String TAG = "MicroMsg.WXEntryActivity";
  25. private IWXAPI api;
  26. private MyHandler handler;
  27. public static String wxAPPID = "wxd9772f42f126413f"; //可以设置成全局变量
  28. private static class MyHandler extends Handler {
  29. private final WeakReference<WXEntryActivity> wxEntryActivityWeakReference;
  30. public MyHandler(WXEntryActivity wxEntryActivity){
  31. wxEntryActivityWeakReference = new WeakReference<WXEntryActivity>(wxEntryActivity);
  32. }
  33. @Override
  34. public void handleMessage(Message msg) {
  35. int tag = msg.what;
  36. }
  37. }
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. api = WXAPIFactory.createWXAPI(this, wxAPPID, false);
  42. handler = new MyHandler(this);
  43. try {
  44. Intent intent = getIntent();
  45. api.handleIntent(intent, this);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. @Override
  51. protected void onNewIntent(Intent intent) {
  52. super.onNewIntent(intent);
  53. setIntent(intent);
  54. api.handleIntent(intent, this);
  55. }
  56. @Override
  57. public void onReq(BaseReq req) {
  58. switch (req.getType()) {
  59. case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
  60. goToGetMsg();
  61. break;
  62. case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
  63. goToShowMsg((ShowMessageFromWX.Req) req);
  64. break;
  65. default:
  66. break;
  67. }
  68. finish();
  69. }
  70. @Override
  71. public void onResp(BaseResp resp) {
  72. int result = 0;
  73. switch (resp.errCode) {
  74. case BaseResp.ErrCode.ERR_OK:
  75. break;
  76. case BaseResp.ErrCode.ERR_USER_CANCEL:
  77. break;
  78. case BaseResp.ErrCode.ERR_AUTH_DENIED:
  79. break;
  80. case BaseResp.ErrCode.ERR_UNSUPPORT:
  81. break;
  82. default:
  83. break;
  84. }
  85. Toast.makeText(this, getString(result) + ", type=" + resp.getType(), Toast.LENGTH_SHORT).show();
  86. if (resp.getType() == ConstantsAPI.COMMAND_SUBSCRIBE_MESSAGE) {
  87. SubscribeMessage.Resp subscribeMsgResp = (SubscribeMessage.Resp) resp;
  88. String text = String.format("openid=%s\ntemplate_id=%s\nscene=%d\naction=%s\nreserved=%s",
  89. subscribeMsgResp.openId, subscribeMsgResp.templateID, subscribeMsgResp.scene, subscribeMsgResp.action, subscribeMsgResp.reserved);
  90. Toast.makeText(this, text, Toast.LENGTH_LONG).show();
  91. }
  92. if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
  93. WXLaunchMiniProgram.Resp launchMiniProgramResp = (WXLaunchMiniProgram.Resp) resp;
  94. String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s",
  95. launchMiniProgramResp.openId, launchMiniProgramResp.extMsg,launchMiniProgramResp.errStr);
  96. Toast.makeText(this, text, Toast.LENGTH_LONG).show();
  97. }
  98. if (resp.getType() == ConstantsAPI.COMMAND_OPEN_BUSINESS_VIEW) {
  99. WXOpenBusinessView.Resp launchMiniProgramResp = (WXOpenBusinessView.Resp) resp;
  100. String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s\nbusinessType=%s",
  101. launchMiniProgramResp.openId, launchMiniProgramResp.extMsg,launchMiniProgramResp.errStr,launchMiniProgramResp.businessType);
  102. Toast.makeText(this, text, Toast.LENGTH_LONG).show();
  103. }
  104. if (resp.getType() == ConstantsAPI.COMMAND_OPEN_BUSINESS_WEBVIEW) {
  105. WXOpenBusinessWebview.Resp response = (WXOpenBusinessWebview.Resp) resp;
  106. String text = String.format("businessType=%d\nresultInfo=%s\nret=%d",response.businessType,response.resultInfo,response.errCode);
  107. Toast.makeText(this, text, Toast.LENGTH_LONG).show();
  108. }
  109. if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
  110. SendAuth.Resp authResp = (SendAuth.Resp)resp;
  111. final String code = authResp.code;
  112. }
  113. finish();
  114. }
  115. private void goToGetMsg() {
  116. // Intent intent = new Intent(this, GetFromWXActivity.class);
  117. // intent.putExtras(getIntent());
  118. // startActivity(intent);
  119. finish();
  120. }
  121. private void goToShowMsg(ShowMessageFromWX.Req showReq) {
  122. WXMediaMessage wxMsg = showReq.message;
  123. WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;
  124. StringBuffer msg = new StringBuffer();
  125. msg.append("description: ");
  126. msg.append(wxMsg.description);
  127. msg.append("\n");
  128. msg.append("extInfo: ");
  129. msg.append(obj.extInfo);
  130. msg.append("\n");
  131. msg.append("filePath: ");
  132. msg.append(obj.filePath);
  133. // Intent intent = new Intent(this, ShowFromWXActivity.class);
  134. // intent.putExtra(Constants.ShowMsgActivity.STitle, wxMsg.title);
  135. // intent.putExtra(Constants.ShowMsgActivity.SMessage, msg.toString());
  136. // intent.putExtra(Constants.ShowMsgActivity.BAThumbData, wxMsg.thumbData);
  137. // startActivity(intent);
  138. finish();
  139. }
  140. }