全局悬浮框(只能在app开启式显示)

本文介绍了如何在Android中通过点击事件启动FloatingButtonService服务,并详细展示了服务内部创建悬浮窗口的代码,包括权限检查、布局管理与事件监听。

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

1.点击事件启动服务

public void startFloatingButtonService(View view) {
        Log.e("TAG", "startFloatingButtonService: "+FloatingButtonService.isStarted);
        if (FloatingButtonService.isStarted) {
            return;
        }
        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
        } else {
            startService(new Intent(MainActivity.this, FloatingButtonService.class));
        }
    }

2.服务代码


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import  com.example.xuanfuactivity.R;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * Created by dongzhong on 2018/5/30.
 */

public class FloatingButtonService extends Service {
    public static boolean isStarted = false;

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private  View mDesktopLayout;
    private Button button,b2;
    private ImageView imageView;

    private int imgs [] ={
            R.mipmap.img1,
            R.mipmap.img2,
            R.mipmap.img3,
            R.mipmap.img4,
            R.mipmap.img5,
            R.mipmap.img6,
            R.mipmap.gif
    };
    private int index=0;
    @Override
    public void onCreate() {
        super.onCreate();
        isStarted = true;
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 500;
        layoutParams.height = 800;
        layoutParams.x = 0;
        layoutParams.y = 0;

    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showFloatingWindow();
        return super.onStartCommand(intent, flags, startId);
    }

    private void showFloatingWindow() {
        if (Settings.canDrawOverlays(this)) {
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mDesktopLayout = inflater.inflate(R.layout.xuanfu_view_diy, null);
            imageView=mDesktopLayout.findViewById(R.id.i1);
            imageView.setImageResource(imgs[0]);
            button=mDesktopLayout.findViewById(R.id.b1);
            button.setOnClickListener(new onClicl());
            b2=mDesktopLayout.findViewById(R.id.b2);
            b2.setOnClickListener(new onClicl());
            mDesktopLayout.setOnTouchListener(new FloatingOnTouchListener());
            windowManager.addView(mDesktopLayout, layoutParams);
        }
    }

    public class onClicl implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.b1:
                    if(index<6){
                        index=index+1;
                    }else{
                        index=0;
                    }
                    imageView.setImageResource(imgs[index]);
                    break;
                case R.id.b2:
                    Log.e("", "onClick: 关闭" );
                    windowManager.removeView(mDesktopLayout);
                    isStarted=false;
                    break;
            }
        }
    }

    private class FloatingOnTouchListener implements View.OnTouchListener, View.OnClickListener, View.OnLongClickListener {
        private int x;
        private int y;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;
                    windowManager.updateViewLayout(view, layoutParams);
                    break;
                default:
                    break;
            }
            return false;
        }

        @Override
        public void onClick(View v) {
            Log.e("TAG", "onClick: " );
        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    }
}

3.服务配置代码

<service
            android:name=".service.FloatingButtonService"
            android:enabled="true"
            android:exported="true" />

4.悬浮框界面(xuanfu_view_diy)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#000099"
    android:layout_gravity="center"
    >
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/i1"
        ></ImageView>

    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="切换下一页"
        android:textColor="#ffffff"></Button>
    <Button
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭悬浮框"
        android:textColor="#ffffff"></Button>
</LinearLayout>

XuanfuActivity.rar-互联网文档类资源-优快云下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值