我正在使用下面的脚本创建一个协程:
fun bar(completion: () -> Unit) {
GlobalScope.launch(Dispatchers.IO) {
val lambda = {
withContext(Dispatchers.Main) { //Suspension functions can be called only within coroutine body
completion()
}
}
foo(lambda)
}
}
fun foo(lambda: () -> Unit) {
//...do something heavy
lambda()
}
Run Code Online (Sandbox Code Playgroud)
但是Suspension functions can be called only within coroutine body当我调用时出现错误,withContext(Dispatchers.Main)因为 lambda 更改了上下文。我无法更改foo(lambda: () -> Unit)为,foo(lambda: suspend () -> Unit)因为它来自外部库。
知道我可以withContext(Dispatchers.Main)在launch上下文中创建的 lambda 内部调用什么吗?
谢谢!
我的应用程序有一个服务,它JobIntentService从AppCompat 扩展类以处理设备上的一些数据并将其发布到HTTPs地址.
代码非常简单,就像互联网/文档中提供的任何样本一样:
我怎么称呼JobIntentService?
(我叫这个FirebaseMessagingService)
val intent = Intent()
intent.putExtra(SOME_INFO, data["someInfo"]?.toInt() ?: INVALID_VALUE_INT)
MyCoolService.enqueueWork(applicationContext, intent)
Run Code Online (Sandbox Code Playgroud)
JobIntentService看起来怎么样?
class MyCoolService: JobIntentService() {
companion object {
val JOB_ID = 1001
fun enqueueWork(context: Context, work: Intent) {
enqueueWork(context, MyCoolService::class.java, JOB_ID, work)
}
}
override fun onHandleWork(intent: Intent) {
//Heavy work here... or empty, doesn't matter... The error happens too.
}
}
Run Code Online (Sandbox Code Playgroud)
表现:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:fullBackupContent="true"
...>
<service android:name="com.my.app.MyCoolService"
android:label="some info service"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<application>
Run Code Online (Sandbox Code Playgroud)
logcat的: …
我遇到列表的读/写数据问题.屏幕上随机位置有一组项目(具有初始位置的顶点).有时我需要在用户触摸屏幕时修改其中一些位置(数量不同).
我第一次实现这个时,我创建了一个translateX列表,并在onDraw方法的每个顶点上应用了这个变换.
我还复制了列表以利用该方法的优势System.arrayCopy来查看我是否得到了任何改进.
有时列表会在写入时读取列表.因此,某些项目的翻译值是错误的.即使使用System.arrayCopy().第一个解决方案是实现一个锁,以便始终等待写入调用方法wait().当写完成时,它会释放wait()调用notify()方法.但这会降低onDraw方法的速度,从而降低FPS.其次,我尝试使用布尔值跳过读取来检查数据是否正在写入.它没有解决.
这是一个例子:
const val COUNT = 100
val xForWrite = FloatArray(COUNT)
val xForRead = FloatArray(COUNT)
val scratch = FloatArray(16)
val translateMatrix = FloatArray(16)
//Called when the user touches the screen and moves the finger on it.
fun updateOffset(value: Float) {
for (i in 0 until COUNT) {
//Translate using value parameter and xForWrite, the translate is not always content and can vary based on each …Run Code Online (Sandbox Code Playgroud) 经过一些研究后,我注意到com.android.support:design:23.3.0使用SVG崩溃的FloatingActionButton android:src.SVG来自Android Studio资产.
它在Android <= 4.4上崩溃.在5.0它完美的工作.
SVG:
ic_keyboard_voice_white_24dp
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M12,15c1.66,0 2.99,-1.34 2.99,-3L15,6c0,-1.66 -1.34,-3 -3,-3S9,4.34 9,6v6c0,1.66 1.34,3 3,3zM17.3,12c0,3 -2.54,5.1 -5.3,5.1S6.7,15 6.7,12L5,12c0,3.42 2.72,6.23 6,6.72L11,22h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
XML:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
app:backgroundTint="@color/colorAccentSecondary"
app:fabSize="normal"
app:elevation="1dp"
android:src="@drawable/ic_keyboard_voice_white_24dp"/>
Run Code Online (Sandbox Code Playgroud)
摇篮:
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
Run Code Online (Sandbox Code Playgroud)
Stacktrace:https://gist.github.com/ppamorim/420dba3e9dc3022a86a3a1f50400d7d0
这是一个错误吗?我解决了这个问题与ImageView使用AppCompatImageView.这个问题的解决方法有FloatingActionButton哪些?