0

I've been trying to create a system overlay targetted towards Android 10 that always displays text on the screen. So far I've been referencing previous StackOverflow threads like the one here: Creating a system overlay window (always on top) and also github: https://gist.github.com/bjoernQ/6975256. My app currently does not work and usually crashes(it says my app keeps closing). The more I read about Android System Overlays online, the more I found out about how code for newer versions differ from older versions, like permissions to put in the Android Manifest and what not. Can anyone here help me find out what is wrong with my code? BTW my eventual goal is to be able to fix an always present, invisible circular seekbar (see here for reference: https://github.com/devadvance/circularseekbar) to the screen of the user, so any tips about seekbars in services would be greatly appreciated, too!

Android Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test">


        <application android:label="Test"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <activity android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <service android:name="com.example.test.VolumeSeekbarOverlay"
                android:exported="false" />
        </application>

        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
        <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    </manifest>

Main Activity:

    package com.example.test;
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Intent service = new Intent(this, VolumeSeekbarOverlay.class);
            startService(service);
            finish();
        }
    }

Service:

    package com.example.test;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.graphics.PixelFormat;
    import android.os.IBinder;
    import android.view.Gravity;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.TextView;

    public class VolumeSeekbarOverlay extends Service {

        private TextView testText;
        private Button overlayedButton;
        private WindowManager wm;

        @Override
        public IBinder onBind(Intent intent) {

            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();

            wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

            testText = new TextView(this);
            testText.setTextColor(Color.BLACK);
            testText.setTextSize(20);
            testText.setText("TEST");

            overlayedButton = new Button(this);
            overlayedButton.setText("Overlay button");
            overlayedButton.setBackgroundColor(Color.BLACK);

            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.LEFT | Gravity.TOP;
            params.x = 0;
            params.y = 0;

            wm.addView(testText, params);
        }

    }

mod3sty
  • 1
  • 1

0 Answers0