[英]How to set drawable on ImageView in Service Class in Android
嘗試在我的服務 class 中的 imageview 上設置可繪制對象,但我找不到導入的可繪制對象。 我可以在活動 class 中設置它。 在服務 class 中是否有特定的方法來執行此操作?
class FloatingVoiceService: Service() {
private var windowManager: WindowManager? = null
private var floatingVoiceDrawable: ImageView? = null
override fun onCreate() {
super.onCreate()
floatingVoiceDrawable = ImageView(this)
floatingVoiceDrawable!!.setImageResource(ContextCompat.getDrawable(this,
R.drawable.ic_record_voice_over_black_24dp))
}
R.drawable.ic_record_voice_over_black_24dp 帶有紅色下划線,因為 IDE 找不到它
這是因為您不能直接從任何其他線程訪問您的 UI/主線程。
您可以使用: runOnUiThread() https://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.