Android中加入悬浮窗

悬浮窗可以一直在前台显示一些信息,无论当前的Activity是哪个应用的。比如显示当前的上下行网速等

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;

/**
 * 悬浮窗Service 该服务会在后台一直运行一个悬浮的透明的窗体。
 *
 * @author
 *
 */
public class FloatingService extends Service {

    private int statusBarHeight;// 状态栏高度
    private View view;// 透明窗体
    private boolean viewAdded = false;// 透明窗体是否已经显示
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

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

    @Override
    public void onCreate() {
        super.onCreate();
        view = LayoutInflater.from(this).inflate(R.layout.floating, null);

        windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
        /*
         * LayoutParams.TYPE_SYSTEM_ERROR:保证该悬浮窗所有View的最上层
         * LayoutParams.FLAG_NOT_FOCUSABLE:该浮动窗不会获得焦点,但可以获得拖动
         * PixelFormat.TRANSPARENT:悬浮窗透明
         */
        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ERROR,
                LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
        // layoutParams.gravity = Gravity.RIGHT|Gravity.BOTTOM; //悬浮窗开始在右下角显示
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;

        view.setOnTouchListener(new OnTouchListener() {
            float[] temp = new float[] { 0f, 0f };

            public boolean onTouch(View v, MotionEvent event) {
                layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
                int eventaction = event.getAction();
                switch (eventaction) {
                case MotionEvent.ACTION_DOWN: // 按下事件,记录按下时手指在悬浮窗的XY坐标值
                    temp[0] = event.getX();
                    temp[1] = event.getY();
                    break;

                case MotionEvent.ACTION_MOVE:
                    refreshView((int) (event.getRawX() - temp[0]), (int) (event
                            .getRawY() - temp[1]));
                    break;

                }
                return true;
            }
        });

    }

    /**
     * 刷新悬浮窗
     *
     * @param x
     *            拖动后的X轴坐标
     * @param y
     *            拖动后的Y轴坐标
     */
    public void refreshView(int x, int y) {
        //状态栏高度不能立即取,不然得到的值是0
        if(statusBarHeight == 0){
            View rootView  = view.getRootView();
            Rect r = new Rect();
            rootView.getWindowVisibleDisplayFrame(r);
            statusBarHeight = r.top;
        }
       
        layoutParams.x = x;
        // y轴减去状态栏的高度,因为状态栏不是用户可以绘制的区域,不然拖动的时候会有跳动
        layoutParams.y = y - statusBarHeight;//STATUS_HEIGHT;
        refresh();
    }

    /**
     * 添加悬浮窗或者更新悬浮窗 如果悬浮窗还没添加则添加 如果已经添加则更新其位置
     */
    private void refresh() {
        if (viewAdded) {
            windowManager.updateViewLayout(view, layoutParams);
        } else {
            windowManager.addView(view, layoutParams);
            viewAdded = true;
        }
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        refresh();
    }

    /**
     * 关闭悬浮窗
     */
    public void removeView() {
        if (viewAdded) {
            windowManager.removeView(view);
            viewAdded = false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeView();
    }
   
   
    class StatusBarReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //intent.get
        }
       
    }
}

 

floating.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   
    >
    <!-- android:updatePeriodMillis="10000"> -->
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/floating"
        android:gravity="center_vertical"
        >
        <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:src="http://blog.163.com/hero_213/blog/@drawable/upload"/>
        <TextView
            android:id="@+id/flowing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="0.5"
            android:gravity="right"
            android:text="0K/s"
            android:textColor="#000000"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:src="http://blog.163.com/hero_213/blog/@drawable/download"/>
        <TextView
            android:gravity="right"
            android:id="@+id/flowspeed"
            android:paddingRight="10.0dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="17K/s"
            android:textColor="#000000"
             />
       
    </LinearLayout>
   
     
   
   
   
    <ImageButton
        android:id="@+id/floating_button_hide"
        android:background="@drawable/floatingwindowhidebutton"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="\?android:buttonStyleSmall" />
</LinearLayout>

 最后还要在AndroidManifest.xml加一个权限

 

      <!-- 悬浮窗(FloatingService) permission -->
      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值