質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

2425閲覧

android 最前面にオーバーレイで画像を表示したい

takori

総合スコア20

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

1クリップ

投稿2022/08/25 10:05

前提

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2022/08/25 12:58

drawable/gazou はどのようなファイルでしょうか。
jimbe

2022/08/25 13:21 編集

androidmanifest.xml にサービスの定義が見当たらないようですが…合っていますか? ボタンを押した時に logcat に「W/ActivityManager: Unable to start service Intent { cmp=[パッケージ名]/.TestService } U=0: not found」といったメッセージは出ていませんか?
takori

2022/08/26 04:41

ありがとうございます。 ・drawable/gazou は800×720のPNG画像(32-bitcolor 14.02kb)です。 ・ボタンを押してもlogcatに「W/ActivityManager: Unable to start service Intent { cmp=[パッケージ名]/.TestService } U=0: not found」というメッセージは表示されなかったです。 一応、実行した後のlogcatはこんな感じです。↓ 2022-08-26 13:38:19.682 4822-4822/io.github.takogori.datausagetest3 W/ziparchive: Unable to open '/data/app/~~PRooV54ur_iH3VRz3GHUjg==/io.github.takogori.datausagetest3-eOaCD2NSnqcZwce0bQP5nw==/base.dm': No such file or directory 2022-08-26 13:38:19.613 4822-4822/? I/.datausagetest: Late-enabling -Xcheck:jni 2022-08-26 13:38:19.641 4822-4822/? D/ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument 2022-08-26 13:38:19.650 4822-4822/? W/re-initialized>: type=1400 audit(0.0:7698): avc: granted { execute } for path="/data/data/io.github.takogori.datausagetest3/code_cache/startup_agents/4ba71210-agent.so" dev="dm-34" ino=135047 scontext=u:r:untrusted_app:s0:c15,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c15,c257,c512,c768 tclass=file app=io.github.takogori.datausagetest3 2022-08-26 13:38:19.657 4822-4822/? V/studio.deploy: No existing instrumentation found. Loading instrumentation from instruments-1f2f27f8.jar 2022-08-26 13:38:19.663 4822-4822/io.github.takogori.datausagetest3 W/.datausagetest: DexFile /data/data/io.github.takogori.datausagetest3/code_cache/.studio/instruments-1f2f27f8.jar is in boot class path but is not in a known location 2022-08-26 13:38:19.664 4822-4822/io.github.takogori.datausagetest3 V/studio.deploy: Applying transforms with cached classes 2022-08-26 13:38:19.670 4822-4822/io.github.takogori.datausagetest3 W/.datausagetest: Redefining intrinsic method java.lang.Thread java.lang.Thread.currentThread(). This may cause the unexpected use of the original definition of java.lang.Thread java.lang.Thread.currentThread()in methods that have already been compiled. 2022-08-26 13:38:19.670 4822-4822/io.github.takogori.datausagetest3 W/.datausagetest: Redefining intrinsic method boolean java.lang.Thread.interrupted(). This may cause the unexpected use of the original definition of boolean java.lang.Thread.interrupted()in methods that have already been compiled. 2022-08-26 13:38:19.673 4822-4822/io.github.takogori.datausagetest3 D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10271; state: ENABLED 2022-08-26 13:38:19.682 4822-4822/io.github.takogori.datausagetest3 W/ziparchive: Unable to open '/data/app/~~PRooV54ur_iH3VRz3GHUjg==/io.github.takogori.datausagetest3-eOaCD2NSnqcZwce0bQP5nw==/base.dm': No such file or directory 2022-08-26 13:38:19.793 4822-4822/io.github.takogori.datausagetest3 V/GraphicsEnvironment: ANGLE Developer option for 'io.github.takogori.datausagetest3' set to: 'default' 2022-08-26 13:38:19.794 4822-4822/io.github.takogori.datausagetest3 V/GraphicsEnvironment: ANGLE GameManagerService for io.github.takogori.datausagetest3: false 2022-08-26 13:38:19.794 4822-4822/io.github.takogori.datausagetest3 V/GraphicsEnvironment: Updatable production driver is not supported on the device. 2022-08-26 13:38:19.796 4822-4822/io.github.takogori.datausagetest3 D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2022-08-26 13:38:19.796 4822-4822/io.github.takogori.datausagetest3 D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2022-08-26 13:38:19.870 4822-4822/io.github.takogori.datausagetest3 W/.datausagetest: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed) 2022-08-26 13:38:19.870 4822-4822/io.github.takogori.datausagetest3 W/.datausagetest: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed) 2022-08-26 13:38:19.942 4822-4842/io.github.takogori.datausagetest3 I/AdrenoGLES-0: QUALCOMM build : 4783c89, I46ff5fc46f Build Date : 11/30/20 OpenGL ES Shader Compiler Version: EV031.31.04.01 Local Branch : QPR2 Remote Branch : Remote Branch : Reconstruct Branch : 2022-08-26 13:38:19.942 4822-4842/io.github.takogori.datausagetest3 I/AdrenoGLES-0: Build Config : S P 10.0.4 AArch64 2022-08-26 13:38:19.942 4822-4842/io.github.takogori.datausagetest3 I/AdrenoGLES-0: Driver Path : /vendor/lib64/egl/libGLESv2_adreno.so 2022-08-26 13:38:19.943 4822-4842/io.github.takogori.datausagetest3 D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument 2022-08-26 13:38:19.946 4822-4842/io.github.takogori.datausagetest3 I/AdrenoGLES-0: PFP: 0x016dd093, ME: 0x00000000 2022-08-26 13:38:19.952 4822-4842/io.github.takogori.datausagetest3 W/AdrenoUtils: <ReadGpuID_from_sysfs:197>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model 2022-08-26 13:38:19.952 4822-4842/io.github.takogori.datausagetest3 W/AdrenoUtils: <ReadGpuID:221>: Failed to read chip ID from gpu_model. Fallback to use the GSL path 2022-08-26 13:38:19.957 4822-4842/io.github.takogori.datausagetest3 D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument 2022-08-26 13:38:49.308 4822-4835/io.github.takogori.datausagetest3 W/System: A resource failed to call close.
takori

2022/08/26 05:11

androidmanifest.xml にサービスを宣言したら表示されました!ありがとうございます。
jimbe

2022/08/26 10:32

> メッセージは表示されなかったです。 そういえばウチの実機は Android10 でしたので、もしかしたらバージョンで表示が違うのかもしれませんね。
guest

回答1

0

自己解決

androidmanifest.xml にサービスを宣言したら表示されました。

投稿2022/08/26 05:11

takori

総合スコア20

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問