android桌面悬浮窗实现-LMLPHP                       android桌面悬浮窗实现-LMLPHP


首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速。好,我们现在就来模拟实现一下类似的效果。

1.新建一个项目 , 打开activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.windowmanagerdemo.MainActivity" >
    
    <Button 
        android:id="@+id/btn_floatWindows"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start Float Window"
        />
</RelativeLayout>
在这里面,只有一个Button ,用来Activity开启悬浮窗服务.

2.打开MainActivity.java
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startFloatWindow=(Button) findViewById(R.id.btn_floatWindows);
        startFloatWindow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, FloatWindowService.class);
                startService(intent);
                finish();
            }
        });
    }

}

里面的代码也很简单,  就是点击Button时跳转到FloatWindowService的服务.

3.接下来新建FloatWindowService.java
public class FloatWindowService extends Service {
    //用于在线程中创建或移除悬浮窗
    private Handler mh=new Handler();
    
    //定时器  定时检测当前应该创建还是移除悬浮窗
    private Timer timer;
    
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //开启定时器  每隔0.5秒刷新一次
        if(timer==null){
            timer=new Timer();
            timer.scheduleAtFixedRate(new RefreshTask(), 0, 500);  //做一个定时任务 每隔500毫秒执行一次
        }
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {  //在Service被关闭时  同时关闭定时任务
        super.onDestroy();
        timer.cancel();
        timer=null;
    }
    
    
    class RefreshTask extends TimerTask{
        @Override
        public void run() {
            //当前界面是窗口 且没有悬浮窗显示, 则创建悬浮窗
            if(isHome()&&!MyWindowManager.isWindowShowing()){
                mh.post(new Runnable() {
                    @Override
                    public void run() {
                        MyWindowManager.createSmallWindow(getApplicationContext());
                    }
                });
            }
            //当前界面不是桌面 且有悬浮窗口显示  则隐藏悬浮窗口
            else if(!isHome()&&MyWindowManager.isWindowShowing()){
                mh.post(new Runnable() {
                    @Override
                    public void run() {
                        MyWindowManager.removeBigWindow(getApplicationContext());
                        MyWindowManager.removeSamllWindow(getApplicationContext());
                    }
                });
            }
            //如果当前是桌面  且有悬浮窗口显示 则更新内存数据
            else if(isHome()&&MyWindowManager.isWindowShowing()){
                mh.post(new Runnable() {
                    @Override
                    public void run() {
                        MyWindowManager.updateUserPercent(getApplicationContext());
                    }
                });
            }
        }
        
    }
    //判断当前界面是否是桌面
    private boolean isHome(){
        ActivityManager mActivityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> rti=mActivityManager.getRunningTasks(1);
        return getHomes().contains(rti.get(0).topActivity.getPackageName());
    }
    
    //获取属于桌面的应用包名称
    private List<String> getHomes(){
        List<String> names=new ArrayList<String>();
        PackageManager packManager=this.getPackageManager();
        Intent intent=new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfo=packManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo ri:resolveInfo) {
            names.add(ri.activityInfo.packageName);
        }
        return names;
    }
}

在这里的onStartCommand()方法中开启一个定时任务,这个定时任务就会每隔500毫秒检查一次悬浮窗的状况
在这里用到了一个MyWindowManager类用于管理悬浮窗.

4.新建一个MyWindowManager.java
public class MyWindowManager {
    //小窗口View的实例
    private static FloatWindowSmallView smallView;
    //大窗口View的实例
    private static FloatWindowBigView bigView;
    //小窗口View的参数
    private static LayoutParams smallViewParams;
    //大窗口View的参数
    private static LayoutParams bigViewParams;
    //用于在屏幕上添加或移除悬浮窗
    private static WindowManager mWindowManager;
    //获取手机可用内存
    private static ActivityManager mActivityManager;
    
    //创建一个小悬浮窗 初始位置为屏幕左边中间
    public static void createSmallWindow(Context context){
        WindowManager windowManager=getWindowManager(context);
        int screenWidth=windowManager.getDefaultDisplay().getWidth();
        int screenHeight=windowManager.getDefaultDisplay().getHeight();
        if(smallView==null){
            smallView=new FloatWindowSmallView(context);
            if(smallViewParams==null){
                smallViewParams=new LayoutParams();
                smallViewParams.type=LayoutParams.TYPE_PHONE;
                smallViewParams.format=PixelFormat.RGBA_8888;
                smallViewParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_NOT_FOCUSABLE;
                smallViewParams.gravity=Gravity.LEFT|Gravity.TOP;
                smallViewParams.width=FloatWindowSmallView.viewWidth;
                smallViewParams.height=FloatWindowSmallView.viewHeight;
                smallViewParams.x=screenWidth;
                smallViewParams.y=screenHeight/2;
            }
            smallView.setParams(smallViewParams);
            windowManager.addView(smallView, smallViewParams);
        }
    }
    //将小窗口从屏幕上移除
    public static void removeSamllWindow(Context context){
        if(smallView!=null){
            WindowManager windowManager=getWindowManager(context);
            windowManager.removeView(smallView);
            smallView=null;
        }
    }
    
    //创建一个大悬浮窗  位于屏幕正中间
    public static void createBigWindow(Context context){
        WindowManager windowManager=getWindowManager(context);
        int screenWidth=windowManager.getDefaultDisplay().getWidth();
        int screenHeight=windowManager.getDefaultDisplay().getHeight();
        if(bigView==null){
            bigView=new FloatWindowBigView(context);
            if(bigViewParams==null){
                bigViewParams = new LayoutParams();
                bigViewParams.x = screenWidth / 2 - FloatWindowBigView.viewWidth / 2;
                bigViewParams.y = screenHeight / 2 - FloatWindowBigView.viewHeight / 2;
                bigViewParams.type = LayoutParams.TYPE_PHONE;
                bigViewParams.format = PixelFormat.RGBA_8888;
                bigViewParams.gravity = Gravity.LEFT | Gravity.TOP;
                bigViewParams.width = FloatWindowBigView.viewWidth;
                bigViewParams.height = FloatWindowBigView.viewHeight;
            }
            windowManager.addView(bigView, bigViewParams);
        }
    }
    
    //将大悬浮窗口从屏幕上移除
    public static void removeBigWindow(Context context){
        if(bigView!=null){
            WindowManager windowManager=getWindowManager(context);
            windowManager.removeView(bigView);
            bigView=null;
        }
    }
    
    //更新小悬浮窗口TextView上的数据
    public static void updateUserPercent(Context context){
        if(smallView!=null){
            TextView percentView=(TextView) smallView.findViewById(R.id.percent);
            percentView.setText(getUserdPercentValue(context));
        }
    }
    
    
    //如果windowManager还未创建,则创建一个新的WindowManager返回  否则返回已经创建的WindowManager
    private static WindowManager getWindowManager(Context context){
        if(mWindowManager==null){
            mWindowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }
    //如果ActivityManager还未创建,则创建一个新的ActivityManager返回 否则返回已经创建了的ActivityManager
    private static ActivityManager getActivityManager(Context context){
        if(mActivityManager==null){
            mActivityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        }
        return mActivityManager;
    }
    
    //获取当前可用内存
    private static long getAvaliableMemory(Context context){
        ActivityManager.MemoryInfo mi=new MemoryInfo();
        getActivityManager(context).getMemoryInfo(mi);
        return mi.availMem;
    }
    
    //计算以使用的内存百分比
    public static String getUserdPercentValue(Context context){
        String dir="/proc/meminfo";
        try {
            FileReader fr=new FileReader(dir);
            BufferedReader br=new BufferedReader(fr);
            String memoryLine=br.readLine();
            String subMemoryLine=memoryLine.substring(memoryLine.indexOf("MemTotal:"));
            br.close();
            long totalMemorySize=Integer.parseInt(subMemoryLine.replaceAll("\\D+", ""));
            long availableSize=getAvaliableMemory(context)/1024;
            int precent=(int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100);  
            return precent+"%";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "悬浮窗";
    }
    
    //是否有悬浮窗口
    public static boolean isWindowShowing(){
        return smallView!=null||bigView!=null;
    }
}
这个方法中包含了很多对悬浮窗的管理方法 ,有添加悬浮窗,删除悬浮窗,更新悬浮窗.
但这个方法里面用到了两个悬浮窗的对象.

5.新建FloatWindowSmallView.java   小悬浮窗的类
public class FloatWindowSmallView extends LinearLayout {
    //记录小窗口的宽度
    public static int viewWidth;
    //记录小窗口的高度
    public static int viewHeight;
    //记录系统状态栏高度
    private static int statusBarHeight;
    //用于更新小悬浮窗位置
    private WindowManager windowManager;
    //小悬浮窗的参数
    private WindowManager.LayoutParams mParams;
    //记录当前手指在屏幕上的横坐标值
    private float xInScreen;
    //记录当前手指在屏幕上的纵坐标值
    private float yInScreen;
    //记录手指在屏幕上按下的横坐标
    private float xDownInScreen;
    //记录手指在屏幕上按下的纵坐标
    private float yDownInScreen;
    //记录手指按下时小悬浮窗的横坐标
    private float xInView;
    //记录手指按下时小悬浮窗的纵坐标
    private float yInView;
    
    
    public FloatWindowSmallView(Context context) {
        super(context);
        windowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.float_windows_small, this);
        View view=findViewById(R.id.small_layout);
        viewHeight=view.getLayoutParams().height;
        viewWidth=view.getLayoutParams().width;
        TextView parcentView=(TextView) findViewById(R.id.percent);
        parcentView.setText(MyWindowManager.getUserdPercentValue(context));
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //手指按下时记录必要的数据  纵坐标的值都需要减去状态栏的高度
            xInView=event.getX();
            yInView=event.getY();  //触摸点相对于控件的左上角位置
            xDownInScreen=event.getRawX();   //触摸点相对于这个屏幕左上角位置
            yDownInScreen=event.getRawY()-getStatusBarHeight();
            xInScreen=event.getRawX();
            yInScreen=event.getRawY()-getStatusBarHeight();
            break;
        case MotionEvent.ACTION_MOVE:
            xInScreen=event.getRawX();
            yInScreen=event.getRawY()-getStatusBarHeight();
            //手指移动时更新悬浮窗的位置
            updateViewPosition();
            break;
        case MotionEvent.ACTION_UP:
            //如果手指离开屏幕  xDownInScreen和xInScreen相同 且 yDownInScreen和yInScreen相等,则视为触发了单机事件
            if(xDownInScreen==xInScreen&&yDownInScreen==yInScreen){
                openBigWindow();
            }
            break;
        default:
            break;
        }
        return true;
    }
    
    //将小悬浮窗的参数传入     用于更新小悬浮窗的位置
    public void setParams(WindowManager.LayoutParams params){
        mParams=params;
    }
    
    //打开大悬浮窗 关闭小悬浮窗
    private void openBigWindow(){
        MyWindowManager.createBigWindow(getContext());
        MyWindowManager.removeSamllWindow(getContext());
    }
    //更新小悬浮窗在屏幕中的位置
    private void updateViewPosition(){
        //在移动的过程中  需要减去触摸点相对于控件的位置   否则就会出现移动时 窗口跟着左上角走, 而不是正中间
        mParams.x=(int) (xInScreen-xInView);
        mParams.y=(int) (yInScreen-yInView);
        windowManager.updateViewLayout(this, mParams);
    }
    
    
    //获取状态栏的高度
    private int getStatusBarHeight(){
        if(statusBarHeight==0){
            try {
                Class<?> c=Class.forName("com.android.internal.R$dimen");
                Object o=c.newInstance();
                Field field=c.getField("status_bar_height");
                int x=field.getInt(o);
                statusBarHeight=getResources().getDimensionPixelSize(x);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return statusBarHeight;
    }
}
在小悬浮窗中动态的显示当前手机所占用的内存,并且可以让用户拖拽窗口,同时实现可以点击效果,当用户点击小悬浮窗时,开启一个大悬浮窗.

6.新建FloatWindowBigView.java  大悬浮窗的类
public class FloatWindowBigView extends LinearLayout {
    //记录大悬浮窗口的宽度
    public static int viewWidth;
    //记录最大悬浮窗的高度
    public static int viewHeight;
    
    
    public FloatWindowBigView(final Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.float_windows_big,this);
        View view=findViewById(R.id.big_layout);
        viewWidth=view.getLayoutParams().width;
        viewHeight=view.getLayoutParams().height;
        Button close=(Button) findViewById(R.id.close);
        Button back=(Button) findViewById(R.id.back);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MyWindowManager.removeBigWindow(context);
                MyWindowManager.removeSamllWindow(context);
                Intent intent=new Intent(getContext(), FloatWindowService.class);
                context.stopService(intent);
            }
        });
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MyWindowManager.removeBigWindow(context);
                MyWindowManager.createSmallWindow(context);
            }
        });
    }
}

大悬浮窗 里面有两个按钮  一个关闭按钮,按下后关闭服务,定时任务取消,去除大悬浮和小悬浮窗口.

最后,还需要在AndroidManifest.xml文件中加入权限.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

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

以及注册悬浮窗的Service

<service android:name=".FloatWindowService"></service>   
android桌面悬浮窗实现-LMLPHP




























05-08 15:28