首页 > 解决方案 > 系统覆盖窗口未按预期工作

问题描述

我正在开发一个可以在所有应用程序顶部绘制一个窗口的应用程序。基本上我想制作一个像夜间模式这样的应用程序。所以为此,我想开发一个可以在所有应用程序之上显示 TextView 的应用程序(只是为了测试我的想法)。我是 android 新手,这是我的第二个应用程序。在学习了 udacity android 基础课程后,我也在自学。

我有一个带有 TextView 的 MainActivity java 文件。当用户触摸它发送到从服务扩展的 OnScreen java 文件的文本视图时。到目前为止,我的应用程序运行良好。它进入 OnScreen 类并制作吐司。在这之后我有两个问题。

我的应用最低 SDK 版本是 17。

清单.xml 文件

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".OnScreen"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

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


    </application>

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

</manifest> 

主要活动


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    public void hello(View v){

        Intent intent = new Intent(this, OnScreen.class);
        startService(intent);

    }
}

屏幕活动

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import static com.prabathhewa.eclips.R.id.dimmer;

public class OnScreen extends Service {
    public OnScreen() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();

        TextView layout = inflater.inflate(R.layout.on_screen, (ViewGroup) findViewById());

        View myView = new View(this);
        WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,

                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,

                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

                PixelFormat.TRANSLUCENT);


        overlay.gravity = Gravity.TOP | Gravity.RIGHT;

        WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
        windowManager.addView(myView, overlay);





    }


}

我想显示的带有文本视图的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dimmer"
    android:background="#7EFFEB3B"
    tools:context=".OnScreen">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="On screen"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:textColor="#FF0505"/>

</LinearLayout>

在问这个之前我读了

Android系统覆盖窗口不起作用

在 Android 中绘制叠加层(系统范围)

创建系统覆盖窗口(始终在顶部)

但我找不到我的问题的确切解决方案。

更新:

我设法通过将layourparams设置为解决了我的第一个问题

WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);

此外,它还帮了我一个忙,它也覆盖了通知栏。在这里,我使用了一个 xml 文件作为我的视图。由哪个定义

LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);

但我不知道如何进入 on_screen.xml 并获取文本视图。

标签: javaandroid

解决方案


我设法通过将layourparams设置为解决了我的第一个问题

WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);

此外,它还帮了我一个忙,它也覆盖了通知栏。在这里,我使用了一个 xml 文件作为我的视图。由哪个定义

LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);

但我不知道如何进入 on_screen.xml 并获取文本视图。


推荐阅读