WindowManager 悬浮窗口

要实现类似于 360 那样的悬浮框,可以使用 WindowManager 来实现


思路

我们再来理清下逻辑,我们需要当处于手机的普通界面,即桌面的时候,悬浮框才显示,而当启动其它 App 时,这个悬浮框应该消失不见,当我们退出 app 又回到桌面,这个悬浮框又要重新出现

我们需要使用一个定时器, 每隔一段时间来进行一系列的判断,比如:是否在桌面,是否已加载悬浮框, 否则加载;否则,如果加载了,就将这个悬浮框移除

实现

  1. 创建一个 空的 Android 项目 cn.twle.android.FloatWindow

  2. 修改 activity_main.xml 添加一个按钮用户开启悬浮框

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
    
        <Button
            android:id="@+id/btn_on"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开启悬浮框" />
    
    </RelativeLayout>
    
  3. 需要一个后台的 Service 在后台等待我们的操作,比如完成悬浮框的绘制移除等,

    因此首先要定义一个 Service 实现一个创建悬浮框 View 的一个方法

    MsMainServer.java

    package cn.twle.android.floatwindow;;
    
    import android.app.ActivityManager;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.graphics.PixelFormat;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.Button;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MsMainService extends Service {
    
        private boolean isAdded = false; // 是否已增加悬浮窗
        public static final int OPERATION_SHOW = 100;
        public static final int OPERATION_HIDE = 101;
        private static final int HANDLE_CHECK_ACTIVITY = 200;
        public static final String OPERATION = "operation";
        private static WindowManager windowManager;
        private static WindowManager.LayoutParams params;
        private Button btnView;
        private ActivityManager mActivityManager;
        private List<String> homeList; // 桌面应用程序包名列表
    
        //定义一个更新界面的Handler
        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HANDLE_CHECK_ACTIVITY:
                        if (isHome()) {
                            if (!isAdded) {
                                windowManager.addView(btnView, params);
                                isAdded = true;
                                new Thread(new Runnable() {
                                    public void run() {
                                        for (int i = 0; i < 10; i++) {
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                            Message m = new Message();
                                            m.what = 2;
                                            mHandler.sendMessage(m);
                                        }
                                    }
                                }).start();
                            }
                        } else {
                            if (isAdded) {
                                windowManager.removeView(btnView);
                                isAdded = false;
                            }
                        }
                        mHandler.sendEmptyMessageDelayed(HANDLE_CHECK_ACTIVITY, 0);
                        break;
                }
            }
        };
    
        @Override
        public void onCreate() {
            super.onCreate();
            homeList = getHomes();
            createWindowView();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int operation = intent.getIntExtra(OPERATION, OPERATION_SHOW);
            switch (operation) {
                case OPERATION_SHOW:
                    mHandler.removeMessages(HANDLE_CHECK_ACTIVITY);
                    mHandler.sendEmptyMessage(HANDLE_CHECK_ACTIVITY);
                    break;
                case OPERATION_HIDE:
                    mHandler.removeMessages(HANDLE_CHECK_ACTIVITY);
                    break;
            }
            return super.onStartCommand(intent, flags, startId);
        }
    
        // 定义一个创建悬浮框的方法:
        private void createWindowView() {
            btnView = new Button(getApplicationContext());
            btnView.setBackgroundResource(R.mipmap.ic_launcher);
            windowManager = (WindowManager) getApplicationContext()
                    .getSystemService(Context.WINDOW_SERVICE);
            params = new WindowManager.LayoutParams();
    
            // 设置Window Type
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            // 设置悬浮框不可触摸
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            // 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应
            params.format = PixelFormat.RGBA_8888;
            // 设置悬浮框的宽高
            params.width = 200;
            params.height = 200;
            params.gravity = Gravity.LEFT;
            params.x = 200;
            params.y = 000;
            // 设置悬浮框的Touch监听
            btnView.setOnTouchListener(new View.OnTouchListener() {
                //保存悬浮框最后位置的变量
                int lastX, lastY;
                int paramX, paramY;
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            lastX = (int) event.getRawX();
                            lastY = (int) event.getRawY();
                            paramX = params.x;
                            paramY = params.y;
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int dx = (int) event.getRawX() - lastX;
                            int dy = (int) event.getRawY() - lastY;
                            params.x = paramX + dx;
                            params.y = paramY + dy;
                            // 更新悬浮窗位置
                            windowManager.updateViewLayout(btnView, params);
                            break;
                    }
                    return true;
                }
            });
            windowManager.addView(btnView, params);
            isAdded = true;
        }
    
        /**
         * 判断当前界面是否是桌面
         */
        public boolean isHome() {
            if (mActivityManager == null) {
                mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            }
            List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
            return homeList.contains(rti.get(0).topActivity.getPackageName());
        }
    
        /**
         * 获得属于桌面的应用的应用包名称
         *
         * @return 返回包含所有包名的字符串列表
         */
        private List<String> getHomes() {
            List<String> names = new ArrayList<String>();
            PackageManager packageManager = this.getPackageManager();
            // 属性
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo ri : resolveInfo) {
                names.add(ri.activityInfo.packageName);
            }
            return names;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    
  4. 修改 Activity_main.xml 添加权限和注册服务

    添加权限

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

    注册服务

    <service android:name=".MsMainService"/>
    
  5. 修改 MainActivity.java 启动 Service

    package cn.twle.android.floatwindow;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btn_on;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bindViews();
        }
    
        private void bindViews() {
            btn_on = (Button) findViewById(R.id.btn_on);
            btn_on.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_on:
                    Intent mIntent = new Intent(MainActivity.this, MsMainService.class);
                    mIntent.putExtra(MsMainService.OPERATION, MsMainService.OPERATION_SHOW);
                    startService(mIntent);
                    Toast.makeText(MainActivity.this, "悬浮框已开启~", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
    

参考文档

  1. 官方 API 文档 : WindowManager

  2. Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果

  3. Android桌面悬浮窗进阶,QQ手机管家小火箭效果实现

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.