_Kotlin_系列_ 三、Kotlin协程(上),flutter消息推送方案

这篇博客深入探讨了 Kotlin 协程的基础知识,包括 runBlocking、launch、suspend 关键字、coroutineScope、async 和 withContext 函数的使用。文章通过实例解释了协程如何在 Android 环境下实现异步操作,并讨论了挂起函数的本质,强调了 Dispatchers 在线程管理中的角色。
摘要由CSDN通过智能技术生成

  • delay 函数是一个非阻塞式挂起函数,它可以让当前协程延迟到指定的时间执行,且只能在协程的作用域或者其他挂起函数中调用

  • 对比 Thread.sleep() 函数,delay 函数只会挂起当前协程,并不会影响其他协程的运行,而 Thread.sleep() 函数会阻塞当前线程,那么该线程下的所有协程都会被阻塞

fun main() {
GlobalScope.launch {
println(“codes run in coroutine scope”)
}
}

上述代码你运行一下会发现日志打印不出来,小朋友,你是否有很多问号?😂

这是因为代码块中的代码还没来得及执行,应用程序就结束了,要解决这个问题,我们可以让程序延迟一段时间在结束,如下:

fun main() {
GlobalScope.launch {
println(“codes run in coroutine scope”)
}
Thread.sleep(1000)
}
//打印结果
codes run in coroutine scope

上述代码我们让主线程阻塞了 1 秒钟在执行,因此代码块中的代码得到了执行。其实这种写法还是存在一点问题,如果我让代码块中的代码在 1 秒钟内不能运行结束,那么就会被强制中断:

fun main() {
GlobalScope.launch {
println(“codes run in coroutine scope”)
delay(1500)
println(“codes run in coroutine scope finished”)
}
Thread.sleep(1000)
}
//打印结果
codes run in coroutine scope

上述代码我们在代码块中加入了一个 delay 函数,并在其之后又打印了一行日志。那么当前协程会挂起 1.5 秒,而主线程却只阻塞了 1 秒,那么重新运行一下程序,新增的这条日志并没有打印出来,因为它还没来得及运行,程序就结束了。

那有办法让协程中所有的代码都执行完了之后在结束吗?🤔️

答:有的,使用 runBlocking 函数

五、使用 runBlocking 函数创建一个能阻塞当前线程的协程作用域

  • runBlocking 函数可以保证在协程作用域内的所有代码和子协程没有全部执行完之前一直阻塞当前线程

注意:runBlocking 函数通常只能在测试环境中使用,在正式环境中使用会容易产生一些性能上的问题

fun main() {
runBlocking {
println(“codes run in coroutine scope”)
delay(1500)
println(“codes run in coroutine scope finished”)
}
}
//打印结果
codes run in coroutine scope
codes run in coroutine scope finished

上述代码我们使用了 runBlocking 函数,可以看到两条日志都能够正常打印出来了。到了这里我心里会有一个疑问:上面的代码都是跑在同一个协程中,我能不能创建多个协程同时跑呢?

答:可以的,使用 launch 函数

六、使用 launch 函数在当前的协程作用域下创建子协程

上面我们讲到过,launch 函数是 CoroutineScope 的一个扩展函数,因此只要拥有协程作用域,就可以调用 launch 函数

  • 单独使用 launch 函数和我们刚才使用的 GlobalScope.launch 函数不同, GlobalScope.launch 创建的是一个顶级协程,而 launch 函数创建的是子协程

fun main() {
runBlocking {
launch {
println(“launch1”)
delay(1000)
println(“launch1 finished”)
}

launch {
println(“launch2”)
delay(1000)
println(“launch2 finished”)
}
}
}
//打印结果
launch1
launch2
launch1 finished
launch2 finished

上述代码我们调用了两次 launch 函数,也就是创建了两个子协程,

`Flutter Android JPush Flutter` 是一个结合了 FlutterAndroid 平台的第推送服务解决方案,通常指的是使用 Alibaba Cloud 的 JPush 在 Flutter 应用中集成推送通知功能。在 Android 端配置推送通知权限,你需要确保遵循 Google Play Store 的政策,并按照以下步骤操作: 1. **添加依赖**: 在 `pubspec.yaml` 文件中添加 JPush 的 Flutter 插件依赖: ```yaml dependencies: jpush_flutter: ^latest_version ``` 替换 `latest_version` 为实际的版本号。 2. **注册应用**: 在 AndroidManifest.xml 中添加 JPush 的 Service 和权限声明: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 其他可能需要的权限 --> <meta-data android:name="JPUSH_CHANNEL" android:value="your_channel_name" /> <service android:name="cn.jpush.android.service.PushService" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service> <service android:name="cn.jpush.android.service.DownloadService" android:exported="false" /> <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> ``` 3. **初始化 JPush**: 在 `MainActivity.kt` 或者适当的生命周期管理类中,初始化 JPush 并设置 AppKey: ```kotlin import com.alibaba.jpush.android.PushManager // 替换为你的 AppKey PushManager.setAppKey("your_app_key") ``` 4. **请求用户授权**: 在适当的地方请求用户的通知权限,例如在启动或首次使用时提示用户: ```kotlin val permissionCheck = ContextCompat.checkSelfPermission( applicationContext, Manifest.permission.VIBRATE ) == PackageManager.PERMISSION_GRANTED if (!permissionCheck) { ActivityCompat.requestPermissions( this, arrayOf(Manifest.permission.VIBRATE), MY_PERMISSIONS_REQUEST_VIBRATE ) } ``` 5. **处理权限结果**: 在 `onRequestPermissionsResult` 方法中处理权限请求的结果,确保通知权限已获取: ```kotlin override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { if (requestCode == MY_PERMISSIONS_REQUEST_VIBRATE) { if (grantResults.isNotEmpty() && grantResults == PackageManager.PERMISSION_GRANTED) { // 用户已授予振动权限,继续配置 JPush } else { // 没有授予,显示错误或提示用户 } } } ``` 完成以上步骤后,你应该就能在 Flutter 应用中正常配置并使用 JPush 的推送通知功能了。如果有其他具体问题,请告诉我,我会提供更详细的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值