現在Androidにて、常に前面にボタンがあるようなアプリを作ろうとしています。
常駐させるためにServiceを使用することにしたのですが
Service#onStartCommand内でWindowManagerにViewを追加した際
TextViewは表示されるのですがImageViewが表示されません…
Activityの中でViewを追加すると表示されるのですが、
なぜこのような挙動になるのか教えていただきたいです。
ソースコード
Kotlin
1class LayerService : Service() { 2 3 private val view : View by lazy { LayoutInflater.from(this).inflate(R.layout.overlay, null) } 4 private val wm : WindowManager by lazy { getSystemService(Context.WINDOW_SERVICE) as WindowManager } 5 6 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 7 val param = WindowManager.LayoutParams( 8 WindowManager.LayoutParams.WRAP_CONTENT, 9 WindowManager.LayoutParams.WRAP_CONTENT, 10 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 11 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 12 PixelFormat.TRANSPARENT 13 ) 14 wm.addView(view, param) 15 return START_STICKY 16 }
overlay.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:text="TextView" /> 14 15 <ImageView 16 android:id="@+id/imageView" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 app:srcCompat="@drawable/sample" /> 20</LinearLayout>
ActivityではstartServiceのみを行っています
どうかよろしくお願いいたします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/03 07:12 編集