WindowManager添加的Floating窗口对startActivity的影响问题解决(Android)

本文探讨了在Android中通过WindowManager添加的浮动按钮启动Activity时遇到的问题。当新Activity需要以透明背景显示时,由于浮动窗口的高优先级导致Activity无法正常显示。解决方法是在点击事件中移除浮动按钮。问题的根源可能是两个透明窗口类型的冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天,通过WindowManager在Activity中添加了一个悬浮button,通过点击button可以启动一个新的Activity.一切都很顺利,悬浮窗口按如下方式就可以添加成功:

      wmParams = new WindowManager.LayoutParams(); 
      //获取的是WindowManagerImpl.CompatModeWrapper 
      mWindowManager = (WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE); 
      Log.i(TAG, "mWindowManager--->" + mWindowManager); 
      //设置window type 
      wmParams.type = LayoutParams.TYPE_PHONE;  
      //设置图片格式,效果为背景透明 
      wmParams.format = PixelFormat.RGBA_8888;  
      //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作) 
      wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;       
      //调整悬浮窗显示的停靠位置为左侧置顶 
      wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;        
      // 以屏幕左上角为原点,设置x、y初始值,相对于gravity 
      wmParams.x = -100; 
      wmParams.y = 0; 

      //设置悬浮窗口长宽数据   
      wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; 
      wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; 

       /*// 设置悬浮窗口长宽数据
      wmParams.width = 200;
      wmParams.height = 80;*/ 
  
      LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      //获取浮动窗口视图所在布局 
      mFloatLayout = inflater.inflate(R.layout.float_button, null); 
     
      ball = (ImageView)mFloatLayout.findViewById(R.id.ball);
      //添加mFloatLayout 
      mWindowManager.addView(mFloatLayout, wmParams);    
     
      mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0, 
              View.MeasureSpec.UNSPECIFIED), View.MeasureSpec 
              .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 

 

但是,现在需要新打开的activity不占满整个屏幕,这样,用户可以看到透明的背景,用户点击透明区域,activity就消失。

设置Activity背景为透明的方法在网上介绍很多,官网也说明,必须通过设置Activity theme来设置。

<activity android:name="com.motorolasolutions.radio.dtapitest.ChannelListActivity"
            android:theme="@android:style/Theme.Dialog" >

按照如上方法设置后,点击浮动button,将不再有任何相应,Activity也打不开。

后来,通过在onClick事件中,删除该浮动button,问题解决。

猜测原因可能是浮动button也是背景透明的窗口,Activity也是背景透明的窗口,而浮动button是TYPE_PHONE类型,所有有更高的显示优先级,所以Acvitiy就显示不出来了。android系统拒绝显示该Acvitiy。不过都是猜测,如果有大侠能够解释或证据,不胜感激。

要实现只有一个悬浮窗没有activity的应用,可以使用Android的系统级别的WindowManager来创建一个悬浮窗,并将其设置为TYPE_SYSTEM_ALERT类型的窗口。这种类型的窗口会显示在其他应用程序的上方,并且不需要添加任何权限。 以下是实现这个功能的代码示例: 1. 创建一个Service类,并在其中使用WindowManager来创建一个悬浮窗。 ``` public class FloatingService extends Service { private WindowManager mWindowManager; private View mFloatingView; @Override public void onCreate() { super.onCreate(); // 创建悬浮窗 mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_view, null); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); mWindowManager.addView(mFloatingView, layoutParams); } @Override public void onDestroy() { super.onDestroy(); // 移除悬浮窗 if (mFloatingView != null) { mWindowManager.removeView(mFloatingView); } } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 2. 在AndroidManifest.xml文件中声明Service,并设置其属性为android:process=":remote",这样可以让Service在一个单独的进程中运行,防止因为应用程序崩溃而导致悬浮窗无法移除。 ``` <service android:name=".FloatingService" android:process=":remote" /> ``` 3. 在启动Service时,可以使用startService()方法来启动Service,并使用stopService()方法来停止Service。这样就可以实现只有一个悬浮窗没有activity的应用了。 ``` // 启动Service Intent intent = new Intent(context, FloatingService.class); context.startService(intent); // 停止Service context.stopService(intent); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值