安卓使用WindowManager开发安卓悬浮窗

Android中我们可以使用WindowManager来生成悬浮窗。WindowManager的三个最常用方法为:

①addView 添加View

addView(View view, WindowManager.LayoutParams params);

View就是要添加到windowmanager中的对象,而params是窗口的设置参数,这个我们讲到代码阶段再说。

②removeView 移除View

removeView(View view);

从windowmanager中移除对象。

③updateViewLayout刷新View

updateViewLayout(View view, ViewGroup.LayoutParams params);

也是两个参数,一个View一个params,参考addView。

下面简单介绍使用方法:

1、Manifest需要添加的权限

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

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

在Android 6.0后,Android需要动态获取权限,要使用权限,不仅仅要在Manifest文件中定义,还要在代码中动态获取

//当AndroidSDK>=23及Android版本6.0及以上时,需要获取OVERLAY_PERMISSION.

//使用canDrawOverlays用于检查,下面为其源码。其中也提醒了需要在manifest文件中添加权限.
 

if (Build.VERSION.SDK_INT >= 23) {

            if (Settings.canDrawOverlays(MainActivity.this)) {

                Intent intent = new Intent(MainActivity.this, MainService.class);

                Toast.makeText(MainActivity.this,"已开启Toucher",Toast.LENGTH_SHORT).show();

                startService(intent);

                finish();

            } else {

                //若没有权限,提示获取.

                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);

                Toast.makeText(MainActivity.this,"需要取得权限以使用悬浮窗",Toast.LENGTH_SHORT).show();

                startActivity(intent);

            }

        } else {

            //SDK在23以下,不用管.

            Intent intent = new Intent(MainActivity.this, MainService.class);

            startService(intent);

            finish();

        }

}


 

2、创建悬浮窗主要代码

在此Demo中,我们可以通过WindowManager来新建一个悬浮窗,悬浮窗的布局通过addView添加、悬浮窗更改位置通过updateViewLayout进行刷新、关闭悬浮窗时调用removeView,跟窗口说再见。
 

private void createToucher(){

        //赋值WindowManager&LayoutParam.

        params = new WindowManager.LayoutParams();

        windowManager = (WindowManager)getApplication().getSystemService(Context.WINDOW_SERVICE);

        //设置type.系统提示型窗口,一般都在应用程序窗口之上.

        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        //设置效果为背景透明.

        params.format = PixelFormat.RGBA_8888;

        //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.

        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        //设置窗口初始停靠位置.

        params.gravity = Gravity.LEFT | Gravity.TOP;

        params.x = 0;

        params.y = 0;

        //设置悬浮窗口长宽数据.

        //注意,这里的width和height均使用px而非dp.这里我偷了个懒

        //如果你想完全对应布局设置,需要先获取到机器的dpi

        //px与dp的换算为px = dp * (dpi / 160).

        params.width = 300;

        params.height = 300;

        LayoutInflater inflater = LayoutInflater.from(getApplication());

        //获取浮动窗口视图所在布局.

        toucherLayout = (ConstraintLayout)inflater.inflate(R.layout.toucherlayout,null);

        //添加toucherlayout

        windowManager.addView(toucherLayout,params);

        Log.i(TAG,"toucherlayout-->left:" + toucherLayout.getLeft());

        Log.i(TAG,"toucherlayout-->right:" + toucherLayout.getRight());

        Log.i(TAG,"toucherlayout-->top:" + toucherLayout.getTop());

        Log.i(TAG,"toucherlayout-->bottom:" + toucherLayout.getBottom());

        //主动计算出当前View的宽高信息.

      toucherLayout.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);

        //用于检测状态栏高度.

        int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");

        if (resourceId > 0){

            statusBarHeight = getResources().getDimensionPixelSize(resourceId);

        }

        Log.i(TAG,"状态栏高度为:" + statusBarHeight);

        //浮动窗口按钮.

        imageButton1 = (ImageButton) toucherLayout.findViewById(R.id.imageButton1);

        //其他代码...

   }

WindowManager使用方法大致介绍完了,在这里也提及下第三方框架FloatWindow,这个框架也可以实现悬浮窗的开发,并且大大简化代码量,有兴趣的可以查找资料学习。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种面向对象的编程语言,而Android Studio是一款用于开发Android应用程序的集成开发环境。悬浮窗是一种可以在屏幕上悬浮显示的窗口,通常用于实现一些常驻的功能或者提醒。在Android应用程序中,可以通过使用悬浮窗实现一些常驻的功能或者提醒,比如音乐播放器的悬浮窗、消息提醒的悬浮窗等等。 要实现Android应用程序中的悬浮窗,可以使用Android提供的WindowManager类。具体来说,可以通过以下步骤来实现: 1. 在AndroidManifest.xml文件中添加SYSTEM_ALERT_WINDOW权限。 2. 创建一个Service,并在其中创建一个悬浮窗View。 3. 在Service中使用WindowManager.addView()方法将悬浮窗View添加到WindowManager中。 4. 在Service中使用WindowManager.removeView()方法将悬浮窗View从WindowManager中移除。 下面是一个简单的示例代码,用于在Android应用程序中实现一个悬浮窗: ``` public class FloatingWindowService extends Service { private WindowManager mWindowManager; private View mFloatingView; public FloatingWindowService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // 创建一个悬浮窗View mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null); // 设置悬浮窗View的参数 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); // 获取WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 将悬浮窗View添加到WindowManager中 mWindowManager.addView(mFloatingView, params); } @Override public void onDestroy() { super.onDestroy(); // 将悬浮窗View从WindowManager中移除 mWindowManager.removeView(mFloatingView); } } ``` 需要注意的是,由于Android 8.0及以上版本对悬浮窗权限进行了限制,因此在这些版本中需要使用TYPE_APPLICATION_OVERLAY类型的WindowManager.LayoutParams来创建悬浮窗。另外,为了保证悬浮窗的显示效果,建议在悬浮窗View中使用透明背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值