Android 在其他应用上悬浮显示View

一个特殊需求,当打开系统wifi设置界面时候,系统界面左上角自带返回按钮,想要的功能是不能点击此返回按钮回到系统设置
界面。
此段代码打开wifi设置界面时,会在左下角和右下角多了返回和下一步按钮,点击这两个按钮都会关闭掉当前的wifi设置界面。

(基于Android 4.2,4.4版本测试)

Intent intent = new Intent();
intent.setAction(Settings.ACTION_WIFI_SETTINGS);
intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
intent.putExtra("extra_prefs_show_button_bar", true); //携带上一步和下一步按钮
intent.putExtra("wifi_enable_next_on_connect", true);
intent.putExtra("extra_prefs_set_back_text", "返回");
startActivity(intent);

开启WIFI 设置界面如下


尝试在Intent中携带各种参数都无效之后,想到在其他应用上显示窗口。搜索了各种方式之后,具体实现方式: 开启一个Service,在service 中获取当前窗体的WindowManager.addView(View view) 添加一个View,通过stopService removeView 来移除view  具体通过开启的activity 生命周期方法来控制显示隐藏

Service 具体实现:

public class ViewService extends Service {


    private WindowManager windowManager;
    private Button button;

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

    @Override
    public void onCreate() {
        super.onCreate();
        showOver();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        windowManager.removeView(button);
    }

    private void showOver() {
        button = new Button(this);

        button.setBackgroundColor(Color.BLACK);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        layoutParams.width = 70;
        layoutParams.height = 50;
        layoutParams.x = 20 ;
        layoutParams.y = 20 ;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP ;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.format = PixelFormat.RGBA_8888 | PixelFormat.TRANSLUCENT;

        windowManager.addView(button, layoutParams);
    }

}

添加权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
添加一个黑色的Button之后(左上角)
 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Android 应用内实现广告悬浮窗,可以使用 Android 的 PopupWindow 类来实现。下面是一个简单的实现方式: 1. 创建一个广告悬浮窗的布局文件,例如 ad_float.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_float_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent"> <!-- 在这里添加你的广告布局 --> </RelativeLayout> ``` 2. 在需要显示广告悬浮窗的地方创建并显示 PopupWindow: ```java // 加载广告悬浮窗的布局文件 View adView = LayoutInflater.from(this).inflate(R.layout.ad_float, null); // 在这里添加你的广告布局 // 创建 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(adView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false); // 设置 PopupWindow 的背景为透明 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置 PopupWindow 的位置 popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0); ``` 3. 在不需要显示广告悬浮窗的地方隐藏 PopupWindow: ```java if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } ``` 注意:在使用 PopupWindow 显示广告悬浮窗时,需要确保不会影响到用户的正常操作,例如不要遮挡到重要的界面元素,也不要让广告悬浮窗一直显示导致用户体验下降。同时,也要遵守广告业界的相关规定,例如不要过于频繁地显示广告等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值