前提
androidの最前面にオーバーレイで画像を表示するアプリをこのページを参考にして作っていますが画像が表示されません。
実現したいこと
最前面にオーバーレイで画像を表示したい。
発生している問題・エラーメッセージ
エラーメッセージはないが画像が表示されない。
該当のソースコード
MainActivity.kt(kotlin)
1package xxx 2 3import android.content.Intent 4import android.net.Uri 5import android.os.Bundle 6import android.provider.Settings 7import android.view.View 8import android.widget.Button 9import android.widget.Toast 10import androidx.activity.result.ActivityResult 11import androidx.activity.result.contract.ActivityResultContracts 12import androidx.appcompat.app.AppCompatActivity 13 14 15class MainActivity : AppCompatActivity() { 16 private var intentService: Intent? = null 17 private val launcher = registerForActivityResult<Intent, ActivityResult>( 18 ActivityResultContracts.StartActivityForResult() 19 ) { result: ActivityResult? -> 20 if (Settings.canDrawOverlays(this)) { 21 startForegroundService(intentService) 22 } else { 23 Toast.makeText( 24 application, R.string.message, 25 Toast.LENGTH_LONG 26 ).show() 27 } 28 } 29 30 override fun onCreate(savedInstanceState: Bundle?) { 31 super.onCreate(savedInstanceState) 32 setContentView(R.layout.activity_main) 33 intentService = Intent(application, TestService::class.java) 34 35 // Serviceを開始するためのボタン 36 val buttonStart = findViewById<Button>(R.id.button_start) 37 buttonStart.setOnClickListener { v: View? -> 38 if (Settings.canDrawOverlays(this)) { 39 startForegroundService(intentService) 40 } else { 41 val intent = Intent( 42 Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 43 Uri.parse("package:$packageName") 44 45 ) 46 startActivity(intent) 47 launcher.launch(intent) 48 } 49 } 50 } 51}
TestService.kt(kotlin)
1package xxx 2 3import android.app.* 4import android.content.Context 5import android.content.Intent 6import android.graphics.PixelFormat 7import android.os.IBinder 8import android.util.Log 9import android.view.* 10 11 12class TestService : Service() { 13 private var context: Context? = null 14 private var notificationManager: NotificationManager? = null 15 private var newView: View? = null 16 private var windowManager: WindowManager? = null 17 override fun onCreate() { 18 super.onCreate() 19 context = applicationContext 20 notificationManager = context!! 21 .getSystemService(NOTIFICATION_SERVICE) as NotificationManager 22 23 // inflaterの生成 24 val layoutInflater = LayoutInflater.from(this) 25 26 // レイアウトファイルからInfalteするViewを作成 27 val nullParent: ViewGroup? = null 28 newView = layoutInflater.inflate(R.layout.service_layer, nullParent) 29 } 30 31 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 32 val channelId = "default" 33 val title = context!!.getString(R.string.app_name) 34 val pendingIntent = PendingIntent.getActivity( 35 context, 0, intent, 36 PendingIntent.FLAG_MUTABLE 37 ) 38 39 // Notification Channel 設定 40 val channel = NotificationChannel( 41 channelId, title, NotificationManager.IMPORTANCE_DEFAULT 42 ) 43 if (notificationManager != null) { 44 notificationManager!!.createNotificationChannel(channel) 45 val notification = Notification.Builder(context, channelId) 46 .setContentTitle(title) // android標準アイコンから 47 .setSmallIcon(android.R.drawable.btn_star) 48 .setContentText("APPLICATION_OVERLAY") 49 .setAutoCancel(true) 50 .setContentIntent(pendingIntent) 51 .setWhen(System.currentTimeMillis()) 52 .build() 53 54 // startForeground 55 startForeground(1, notification) 56 } 57 val typeLayer = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 58 windowManager = applicationContext 59 .getSystemService(WINDOW_SERVICE) as WindowManager 60 val params = WindowManager.LayoutParams( 61 WindowManager.LayoutParams.WRAP_CONTENT, 62 WindowManager.LayoutParams.WRAP_CONTENT, 63 typeLayer, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 64 or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 65 PixelFormat.TRANSLUCENT 66 ) 67 68 // dipを取得 69 val dpScale = resources.displayMetrics.density.toInt() 70 71 // 右上に配置 72 params.gravity = Gravity.TOP or Gravity.END 73 params.x = 20 * dpScale // 20dp 74 params.y = 80 * dpScale // 80dp 75 76 // ViewにTouchListenerを設定する 77 newView!!.setOnTouchListener { v: View?, event: MotionEvent -> 78 Log.d("debug", "onTouch") 79 if (event.action == MotionEvent.ACTION_DOWN) { 80 Log.d("debug", "ACTION_DOWN") 81 } 82 if (event.action == MotionEvent.ACTION_UP) { 83 Log.d("debug", "ACTION_UP") 84 85 // warning: override performClick() 86 newView!!.performClick() 87 88 // Serviceを自ら停止させる 89 stopSelf() 90 } 91 false 92 } 93 94 // Viewを画面上に追加 95 windowManager!!.addView(newView, params) 96 return super.onStartCommand(intent, flags, startId) 97 } 98 99 override fun onDestroy() { 100 super.onDestroy() 101 Log.d("debug", "onDestroy") 102 // Viewを削除 103 windowManager!!.removeView(newView) 104 } 105 106 override fun onBind(intent: Intent): IBinder? { 107 return null 108 } 109}
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:gravity="center" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/button_start" 12 android:text="@string/button" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" /> 15 16</LinearLayout>
service_layer.xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content"> 5 6 <ImageView 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:contentDescription="@string/description" 10 android:src="@drawable/gazou" /> 11 12</RelativeLayout>
AndroidManifest.xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 package="io.github.takogori.datausagetest3"> 5 6 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 7 8 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 9 10 <application 11 android:allowBackup="true" 12 android:dataExtractionRules="@xml/data_extraction_rules" 13 android:fullBackupContent="@xml/backup_rules" 14 android:icon="@mipmap/ic_launcher" 15 android:label="@string/app_name" 16 android:roundIcon="@mipmap/ic_launcher_round" 17 android:supportsRtl="true" 18 android:theme="@style/Theme.Datausagetest3" 19 tools:targetApi="31"> 20 <activity 21 android:name=".MainActivity" 22 android:exported="true"> 23 <intent-filter> 24 <action android:name="android.intent.action.MAIN" /> 25 26 <category android:name="android.intent.category.LAUNCHER" /> 27 </intent-filter> 28 </activity> 29 </application>
補足情報(FW/ツールのバージョンなど)
Android Studioのバージョン:Android Studio Chipmunk | 2021.2.1 Patch 2
使用している言語:kotlin
min sdk version:API26 Android8.0 (Marshmallow)
使用している実機:Pixel5a(android12)

回答1件
あなたの回答
tips
プレビュー