I also want to filter the status bar.

Asked 2 years ago, Updated 2 years ago, 115 views

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="add Filter"
    android: id="@+id/ButtonAddFilter"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove Filter"
    android: id="@+id/ButtonRemoveFilter"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>
</RelativeLayout>

MainActivity.java

public class MainActivity extensions Activity {

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

    Button ButtonAddFilter= (Button) findViewById (R.id.ButtonAddFilter);
    buttonAddFilter.setOnClickListener(newView.OnClickListener(){
        @ Override
        public void onClick (View v) {
            Intent = new Intent (MainActivity.this, FilterService.class);
            startService(intent);
        }
    });

    Button ButtonRemoveFilter= (Button) findViewById (R.id.ButtonRemoveFilter);
    buttonRemoveFilter.setOnClickListener(newView.OnClickListener(){
        @ Override
        public void onClick (View v) {
            Intent = new Intent (MainActivity.this, FilterService.class);
            stopService(intent);
        }
    });
}
}

filter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

FilterService.java

public class FilterService extensions Service {

private View mView;
private WindowManager mWindowManager;

@ Override
public int onStartCommand(Intent intint, int flags, int startId) {

    LayoutInflater layoutInflater=LayoutInflater.from(this);

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,    
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT                       
    );

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

    mView = layoutInflater.inflate(R.layout.filter, null);

    mView.setBackgroundColor(Color.argb(80,0,0));

    mWindowManager.addView (mView, layoutParams);

    return START_NOT_STICKY;
}

@ Override
public void onDestroy() {
    super.onDestroy();
    mWindowManager.removeView(mView);
}

@ Nullable
@ Override
public IBinder onBind (Intent int) {
    return null;
}
}

This is the process of filtering the screen.In this case, the status bar is not dimmed.
Is there any way to reflect mWindowManager to the status bar or darken the status bar so that it doesn't feel uncomfortable?

android java android-studio android-layout

2022-09-30 11:43

1 Answers

Try this.

WindowManager.LayoutParams layoutParams=newWindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(),
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);

    layoutParams.gravity=Gravity.TOP | Gravity.CENTER;      

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

    mView = layoutInflater.inflate(R.layout.filter, null);

    mView.setBackgroundColor(Color.argb(80,0,0));

    mWindowManager.addView (mView, layoutParams);

private static int getStatusBarHeight(android.content.res.Resources){
    return(int)(24*res.getDisplayMetrics().density);
}

I won't forget Permission.

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


2022-09-30 11:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.