【flutter】Flutter项目集成第三方sdk总结
实现方式
- 在Android层启动sdk
- 在flutter代码中 通过原生通信执行sdk集成的方法
一、在Android层集成SDK(kotlin)
说明:一般sdk集成会要求设置集成模式后启动sdk,因此使用attachBaseContext
和onCreate
来实现SDK集成。
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
Log.i(TAG, "----------设置SDK的使用模式--------")
SDK.setMode();
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "----------开始启动隧道--------")
SDK.start(application ) { response ->
Log.i("TgentSDK", response.toString())
//80:隧道启动成功
//81:隧道启动失败
if (response.code == 80) {
Log.i(TAG, "----------隧道启动成功--------")
//隧道启动成功,进入App逻辑
}else{
Log.i(TAG, "----------隧道启动失败--------")
}
//其他错误码见文档
}
// 你的初始化代码...
}
注意:application
在attachBaseContext方法中可能为null,因此如果初始化或者sdk.start需要使用application
,在kotlin中需要在onCreate
方法中实现 。
二、在flutter代码层调用sdk实现sdk启动
说明:在flutter仅是通过原生通信执行.kt或.java的l实现方法来实现sdk集成
- 首先需要写好调用第三方sdk的tool工具类
//tool工具类部分代码
class SFSDKPlugin_c : AppCompatActivity() , FlutterPlugin, MethodChannel.MethodCallHandler,EventChannel.StreamHandler{
//定义通信
var METHOD_CHANNEL = "sdp_method_channel"
var EVENT_CHANNEL = "sdp_event_channel"
private val TAG = "SFApplication-Plugin"
private lateinit var channel: MethodChannel
private lateinit var eventChannel: EventChannel
private var sink: EventChannel.EventSink? = null
private val handler: Handler = Handler(Looper.getMainLooper())
//注册方法
companion object {
private val TAG = "SFApplication-Plugin"
fun registerWith(registrar: FlutterEngine) {
try {
registrar.plugins.add(SFSDKPlugin_c())
} catch (e: Exception) {
Log.e(TAG, "Error registering plugin SFSDKPlugin_c", e)
}
}
}
//设置通信监听及sdk初始化方法
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(binding.binaryMessenger, METHOD_CHANNEL)
channel.setMethodCallHandler(this)
eventChannel = EventChannel(binding.binaryMessenger, EVENT_CHANNEL)
eventChannel.setStreamHandler(this)
this.sContext = binding.applicationContext
/**
* sdk初始化方法
*/
Log.i(TAG, "----------设置认证回调--------")
//SDK...
}
//通信
override fun onDetachedFromEngine(p0: FlutterPluginBinding) {
channel.setMethodCallHandler(null)
eventChannel.setStreamHandler(null)
}
//消息发送
private fun sendEvent(event: MyResultData) {
val resultMap = mapOf(
"result" to event.result,
"accessToken" to event.accessToken,
"cookieSessionId" to event.cookieSessionId
)
handler.post {
sink?.success(resultMap)
}
}
//定义通信事件
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: MethodChannel.Result) {
when (call.method) {
"startAuth" -> {
Log.i(TAG, "----------开始认证:$userName---服务器地址:$serverUrl--------
//SDK方法实现
result.success(null)
}
"startAutoTicket" -> {
startAutoTicket()
}
else -> result.notImplemented()
}
}
}
- 将tool工具类注册到MainActivity中
class MainActivity: FlutterActivity() {
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
sContext = base
/**
* 初始化sdk,
*/
Log.i(TAG, "----------开始初始化SOK--------")
//初始化sdk
initSdk(base)
/**
* 注销事件监听回调,推荐在Application里面监听, 避免出现认证开始了还未监听的问题
*/
Log.i(TAG, "----------注销事件监听回调--------")
SDKLogoutListener { type, message ->
SFLogN.info(
TAG,
"onLogout, message: $message"
)
}
}
//注册插件
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
SFSDKPlugin_c.registerWith(flutterEngine)
}
}
- 在flutter代码中通过通信调用tool里方法
//dart里原生通信部分代码
abstract class SFSDKTools {
static const MethodChannel _methodChannel = const MethodChannel("sdp_method_channel");
static const EventChannel _eventChannel = const EventChannel("sdp_event_channel");
//调用原生里写的方法
static Future<void> startAuth(userName,password) async {
//一些参数获取
await _methodChannel.invokeMethod('startAuth');
}
//监听 native event 数据流
static void onListenStreamData(onEvent, onError) {
_eventChannel.receiveBroadcastStream().listen(onEvent, onError: onError);
}
//事件流处理
static void onReceiveEvent(context) {
_context = context;
onListenStreamData((data) {
switch (data['result']) {
case "Auth_SUCCESS":
onSuccessLogin();
break;
case "Auth_Failed":{
onAuthFailed();
}
break;
case "Auth_Progress":
{
print("二次认证");
Fluttertoast.showToast(msg: "请进行二次认证",textColor: Colors.white,backgroundColor: Colors.red,gravity: ToastGravity.CENTER);
}
break;
case "Auth_SUCCESS_NOPWD"://免密认证成功 app自主登录
{
onSuccessLogin();
}
break;
case "Auth_NOPWD_Failed"://免密认证失败 app认证登录
{
onAuthNOPWDFailedLogin();
}
break;
}
}, (error) {
print("event channel error : $error");
});
}
}
原文地址:https://blog.csdn.net/qq_44143906/article/details/144290919
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!