Android浮动窗口

时间:2018-03-25 11:07:26

标签: android android-studio

我想制作一个红色叠加窗口。你可以在下面看到我的代码。当我点击按钮它没有做任何事情。问题出在哪儿?我怎样才能解决这个问题。请提出建议 谢谢!

public class MainActivity extends AppCompatActivity {
Button button;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, OverlayLayer.class);
            startService(intent);
        }
    });
}
}


public class OverlayLayer extends Service {

public IBinder onBind(Intent ıntent) {
    return null;
}
private LinearLayout mLayout;
private WindowManager mManager;


@Override
public void onCreate() {
    super.onCreate();
    mManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    mLayout = new LinearLayout(this);
    LinearLayout.LayoutParams mParams=new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    mLayout.setBackgroundColor(Color.rgb(255,255,0));
    mLayout.setLayoutParams(mParams);
    WindowManager.LayoutParams parameters=new WindowManager.LayoutParams(400,150,WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);
    parameters.x=0;
    parameters.y=0;
    parameters.gravity = Gravity.CENTER;
    mManager.addView(mLayout,parameters);
   }
 }

1 个答案:

答案 0 :(得分:0)

首先在AndroidManifest中添加你的服务,使其成为“静态”,因为它在你的MainActivity.java文件中:

<service android:name=".MainActivity$OverlayLayer"/>

//make class static
public static class OverlayLayer extends Service {
...
}

或者创建一个独立的(与MainActivity分开)类,如OverlayLayer.java,并将其添加到Manifest文件中,如:

<service android:name=".OverlayLayer"/>

之后使用以下代码在窗口中添加视图:

在窗口上添加视图的标志取决于操作系统,因此下面的代码将使其适用于每个操作系统:

OverlayLayer 类:

  @Override
        public void onCreate() {
            super.onCreate();
            mManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            mLayout = new LinearLayout(this);
            LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            mLayout.setBackgroundColor(Color.rgb(255, 255, 0));
            mLayout.setLayoutParams(mParams);


            addToWindow(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }


        public void addToWindow(int LAYOUT_FLAG) {
            WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(400, 150, LAYOUT_FLAG, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
            parameters.x = 0;
            parameters.y = 0;
            parameters.gravity = Gravity.CENTER;
            try {

                mManager.addView(mLayout, parameters);

            } catch (Exception e) {

                if (LAYOUT_FLAG == WindowManager.LayoutParams.TYPE_SYSTEM_ALERT) {
                    addToWindow(WindowManager.LayoutParams.TYPE_TOAST);
                }
                if (LAYOUT_FLAG == WindowManager.LayoutParams.TYPE_TOAST) {
                    addToWindow(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                }
                if (LAYOUT_FLAG == WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY) {
                    addToWindow(WindowManager.LayoutParams.TYPE_PHONE);
                }
            }
        }