您的位置:首页 > 娱乐 > 明星 > 泰安网络软件公司_怎样制作一个app软件_小程序开发流程详细_免费网站seo

泰安网络软件公司_怎样制作一个app软件_小程序开发流程详细_免费网站seo

2024/12/22 1:03:13 来源:https://blog.csdn.net/qq_44143906/article/details/144290919  浏览:    关键词:泰安网络软件公司_怎样制作一个app软件_小程序开发流程详细_免费网站seo
泰安网络软件公司_怎样制作一个app软件_小程序开发流程详细_免费网站seo

实现方式

  1. 在Android层启动sdk
  2. 在flutter代码中 通过原生通信执行sdk集成的方法

一、在Android层集成SDK(kotlin)

说明:一般sdk集成会要求设置集成模式后启动sdk,因此使用attachBaseContextonCreate来实现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集成

  1. 首先需要写好调用第三方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: MethodChannelprivate lateinit var eventChannel: EventChannelprivate var sink: EventChannel.EventSink? = nullprivate 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()}}
}
  1. 将tool工具类注册到MainActivity中
class MainActivity: FlutterActivity() {override fun attachBaseContext(base: Context) {super.attachBaseContext(base)sContext = base/*** 初始化sdk,*/Log.i(TAG, "----------开始初始化SOK--------")//初始化sdkinitSdk(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)}
}
  1. 在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");});}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com