android快速创建悬浮窗

代码如下

记录

package com.pdd.xuanfu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintHelper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {
    int REQUEST_CODE = 1;
    WindowManager windowManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(! isCanDrawOverlays(this)){
            requestAlertWindowPermission();
        }
        showFloatWindow();
    }

    //判断权限
    private boolean isCanDrawOverlays(Context context) {
        Boolean result = true;
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class clazz = Settings.class;
                Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                result = (Boolean) canDrawOverlays.invoke(null, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //申请权限
    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + MainActivity.this.getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
    }


    public void showFloatingWindow(View v) {
        Toast.makeText(this, "已点击", Toast.LENGTH_SHORT).show();
    }


    private void showFloatWindow() {


        TextView tv = new TextView(this);
        tv.setText("66666666666666");
//        tv.setBackgroundColor(Color.RED);


        int LAYOUT_FLAG;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            System.out.println("666");
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }



        windowManager = (WindowManager) getSystemService(this.WINDOW_SERVICE);

        WindowManager.LayoutParams mFlowViewLayoutParams =  new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                0,-100,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.
                         FLAG_NOT_FOCUSABLE
                        // 加上这句话悬浮窗不拦截事件
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
//            mFlowViewLayoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
//        }
//
//        mFlowViewLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_FULLSCREEN;

        windowManager.addView(tv, mFlowViewLayoutParams);
    }


}

 

posted @ 2022-09-09 16:12  sunny开始学坏  阅读(339)  评论(0编辑  收藏  举报