微信分享配置回调Activity中android:exported="true"的一个坑

在做微信分享的时候,需要用到一个微信的回调Activity:WXEntryActivity

用的是百度的frontia的分享功能

需要用到微信的jar包,可以去开放平台获取

public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler {

这个activity需要实现一个接口,接口回调函数

// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			if(wxresp!=null)
			wxresp.onSuccess();
			Toast.makeText(this, "发送成功", Toast.LENGTH_LONG).show();
			finish();
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			if(wxresp!=null)
			wxresp.onFail();
			Toast.makeText(this, "分享取消", Toast.LENGTH_LONG).show();
			finish();
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			Toast.makeText(this, "分享被拒绝", Toast.LENGTH_LONG).show();
			finish();
			break;
		default:
			Toast.makeText(this, "分享返回", Toast.LENGTH_LONG).show();
			break;
		}

	}

最后需要注意的是在manifest里面配置这个activity,必须配置启动模式为singleInstance,这个启动模式会开辟另外一个栈来启动,因为要调用微信客户端里的界面,所以是在另外一个栈来实现,如果对这个模式不是很了解,个人觉得郭霖的第一行代码里面讲的很明了,推荐看看

<activity android:name="com.xxx.xxx.wxapi.WXEntryActivity"
            android:launchMode="singleInstance"
            android:exported="true"
            android:windowSoftInputMode="adjustPan|adjustUnspecified|stateHidden"
            android:screenOrientation="portrait"/>

这样这个回调activity才会回调。之前一直没回调就是因为没配置 singleInstance!!!


之前理解错误了,以为是没配置singleInstance,其实是没配置android:exported="true"这个,尴尬了!!!!!误导大家了,抱歉!

不过很奇怪我用的百度的frontia分享组件,微信分享需要额外的一个activity,腾讯qq分享不需要额外的activity,但两个平台都需要到相应地方通过审核拿到key才能用


已标记关键词 清除标记
``` 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(); } } ```
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页