效果
要求
文档
开发标签文档
android接入指南
android实现
1.接入openSDK
dependencies {
api ‘com.tencent.mm.opensdk:wechat-sdk-android:6.8.0’
}
2.注册
需要在包名为(applicationId)+wxapi的包下加入以上几个类。
类名 | 功能 |
---|---|
AppRegister | 是一个广播接受者,用于注册微信sdk |
ShareSDKWeChatActivity | 封装了shareSDK分享需要的功能 |
WXConstants | 存放了微信的app_id,这个id就是shareSDK配置时填写的id |
WXEntryActivity | 给微信的回调。 |
3.相关实现.
AppRegister
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
public class AppRegister extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final IWXAPI api = WXAPIFactory.createWXAPI(context, null,false);
// 将该app注册到微信
api.registerApp(WXConstants.APP_ID);
}
}
对应的清单文件
<receiver
android:name="com.wznews.news.app.wxapi.AppRegister"
android:permission="com.tencent.mm.plugin.permission.SEND">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
</intent-filter>
</receiver>
ShareSDKWeChatActivity
import cn.sharesdk.wechat.utils.WechatHandlerActivity;
/**
* <pre>
* Created by zhuguohui
* Date: 2022/11/16
* Time: 11:37
* Desc:这是继承自ShareSDK的微信回调累。避免我们的WXEntryActivity将ShareSDK的相关功能给覆盖
* </pre>
*
*/
public class ShareSDKWeChatActivity extends WechatHandlerActivity {
}
对应的清单文件
没有
WXConstants
/**
* Created by zhuguohui
* Date: 2022/11/16
* Time: 10:37
* Desc:
*/
public interface WXConstants {
String APP_ID="wx123456";
}
WXEntryActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.trs.v6.ad.splash.SplashActivityV6;
import org.json.JSONObject;
/**
* Created by zhuguohui
* Date: 2022/11/16
* Time: 10:07
* Desc:
*/
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, WXConstants.APP_ID, false);
try {
Intent intent = getIntent();
api.handleIntent(intent, this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq req) {
String ext = ((ShowMessageFromWX.Req) req).message.messageExt;
//以下代码需要替换为自己的业务逻辑
try {
JSONObject jsonObject = new JSONObject(ext);
String url = jsonObject.getString("url");
SplashActivityV6.openUrl(this, url);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onResp(BaseResp resp) {
// finish();
//把原来ShareSDK的微信回调类WXEntryActivity改成SSDKWXEntryActivity
//然后在AndroidManifest中,注册SSDKWXEntryActivity
Intent intent = new Intent(this, ShareSDKWeChatActivity.class);
intent.putExtras(getIntent());
startActivity(intent);
finish();
}
}
对应的清单文件
<activity
android:name="com.wznews.news.app.wxapi.WXEntryActivity"
android:exported="true"
android:launchMode="singleTask"
android:taskAffinity="${applicationId}"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />