android 悬浮窗

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

 

Java代码   收藏代码
  1. import android.app.Service;  
  2. import android.content.BroadcastReceiver;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.graphics.PixelFormat;  
  6. import android.graphics.Rect;  
  7. import android.os.IBinder;  
  8. import android.view.Gravity;  
  9. import android.view.LayoutInflater;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.view.WindowManager;  
  13. import android.view.View.OnTouchListener;  
  14. import android.view.WindowManager.LayoutParams;  
  15.   
  16. /** 
  17.  * 悬浮窗Service 该服务会在后台一直运行一个悬浮的透明的窗体。 
  18.  *  
  19.  * @author 
  20.  *  
  21.  */  
  22. public class FloatingService extends Service {  
  23.   
  24.     private int statusBarHeight;// 状态栏高度  
  25.     private View view;// 透明窗体  
  26.     private boolean viewAdded = false;// 透明窗体是否已经显示  
  27.     private WindowManager windowManager;  
  28.     private WindowManager.LayoutParams layoutParams;  
  29.   
  30.     @Override  
  31.     public IBinder onBind(Intent intent) {  
  32.         return null;  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onCreate() {  
  37.         super.onCreate();  
  38.         view = LayoutInflater.from(this).inflate(R.layout.floating, null);  
  39.   
  40.         windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);  
  41.         /* 
  42.          * LayoutParams.TYPE_SYSTEM_ERROR:保证该悬浮窗所有View的最上层 
  43.          * LayoutParams.FLAG_NOT_FOCUSABLE:该浮动窗不会获得焦点,但可以获得拖动 
  44.          * PixelFormat.TRANSPARENT:悬浮窗透明 
  45.          */  
  46.         layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,  
  47.                 LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ERROR,  
  48.                 LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);  
  49.         // layoutParams.gravity = Gravity.RIGHT|Gravity.BOTTOM; //悬浮窗开始在右下角显示  
  50.         layoutParams.gravity = Gravity.LEFT | Gravity.TOP;  
  51.   
  52.         view.setOnTouchListener(new OnTouchListener() {  
  53.             float[] temp = new float[] { 0f, 0f };  
  54.   
  55.             public boolean onTouch(View v, MotionEvent event) {  
  56.                 layoutParams.gravity = Gravity.LEFT | Gravity.TOP;  
  57.                 int eventaction = event.getAction();  
  58.                 switch (eventaction) {  
  59.                 case MotionEvent.ACTION_DOWN: // 按下事件,记录按下时手指在悬浮窗的XY坐标值  
  60.                     temp[0] = event.getX();  
  61.                     temp[1] = event.getY();  
  62.                     break;  
  63.   
  64.                 case MotionEvent.ACTION_MOVE:  
  65.                     refreshView((int) (event.getRawX() - temp[0]), (int) (event  
  66.                             .getRawY() - temp[1]));  
  67.                     break;  
  68.   
  69.                 }  
  70.                 return true;  
  71.             }  
  72.         });  
  73.   
  74.     }  
  75.   
  76.     /** 
  77.      * 刷新悬浮窗 
  78.      *  
  79.      * @param x 
  80.      *            拖动后的X轴坐标 
  81.      * @param y 
  82.      *            拖动后的Y轴坐标 
  83.      */  
  84.     public void refreshView(int x, int y) {  
  85.         //状态栏高度不能立即取,不然得到的值是0  
  86.         if(statusBarHeight == 0){  
  87.             View rootView  = view.getRootView();  
  88.             Rect r = new Rect();  
  89.             rootView.getWindowVisibleDisplayFrame(r);  
  90.             statusBarHeight = r.top;  
  91.         }  
  92.           
  93.         layoutParams.x = x;  
  94.         // y轴减去状态栏的高度,因为状态栏不是用户可以绘制的区域,不然拖动的时候会有跳动  
  95.         layoutParams.y = y - statusBarHeight;//STATUS_HEIGHT;  
  96.         refresh();  
  97.     }  
  98.   
  99.     /** 
  100.      * 添加悬浮窗或者更新悬浮窗 如果悬浮窗还没添加则添加 如果已经添加则更新其位置 
  101.      */  
  102.     private void refresh() {  
  103.         if (viewAdded) {  
  104.             windowManager.updateViewLayout(view, layoutParams);  
  105.         } else {  
  106.             windowManager.addView(view, layoutParams);  
  107.             viewAdded = true;  
  108.         }  
  109.     }  
  110.   
  111.     @Override  
  112.     public void onStart(Intent intent, int startId) {  
  113.         super.onStart(intent, startId);  
  114.         refresh();  
  115.     }  
  116.   
  117.     /** 
  118.      * 关闭悬浮窗 
  119.      */  
  120.     public void removeView() {  
  121.         if (viewAdded) {  
  122.             windowManager.removeView(view);  
  123.             viewAdded = false;  
  124.         }  
  125.     }  
  126.   
  127.     @Override  
  128.     public void onDestroy() {  
  129.         super.onDestroy();  
  130.         removeView();  
  131.     }  
  132.       
  133.       
  134.     class StatusBarReceiver extends BroadcastReceiver{  
  135.   
  136.         @Override  
  137.         public void onReceive(Context context, Intent intent) {  
  138.             //intent.get  
  139.         }  
  140.           
  141.     }  
  142. }  
 

floating.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"   
  4.     android:layout_width="wrap_content"   
  5.     android:layout_height="wrap_content"   
  6.       
  7.     >  
  8.     <!-- android:updatePeriodMillis="10000"> -->  
  9.       
  10.     <LinearLayout   
  11.         android:orientation="horizontal"   
  12.         android:layout_width="wrap_content"   
  13.         android:layout_height="wrap_content"  
  14.         android:background="@drawable/floating"   
  15.         android:gravity="center_vertical"  
  16.         >  
  17.         <ImageView  
  18.         android:layout_width="wrap_content"   
  19.         android:layout_height="wrap_content"   
  20.         android:layout_weight="0.5"   
  21.         android:src="@drawable/upload"/>  
  22.         <TextView   
  23.             android:id="@+id/flowing"   
  24.             android:layout_width="wrap_content"   
  25.             android:layout_height="wrap_content"    
  26.             android:layout_weight="0.5"   
  27.             android:gravity="right"  
  28.             android:text="0K/s"  
  29.             android:textColor="#000000"  
  30.             />  
  31.   
  32.         <ImageView  
  33.             android:layout_width="wrap_content"   
  34.             android:layout_height="wrap_content"   
  35.             android:layout_weight="0.5"   
  36.             android:src="@drawable/download"/>  
  37.         <TextView   
  38.             android:gravity="right"   
  39.             android:id="@+id/flowspeed"   
  40.             android:paddingRight="10.0dip"   
  41.             android:layout_width="wrap_content"   
  42.             android:layout_height="wrap_content"   
  43.             android:layout_weight="0.5"  
  44.             android:text="17K/s"  
  45.             android:textColor="#000000"  
  46.              />  
  47.           
  48.     </LinearLayout>  
  49.       
  50.         
  51.       
  52.       
  53.       
  54.     <ImageButton   
  55.         android:id="@+id/floating_button_hide"   
  56.         android:background="@drawable/floatingwindowhidebutton"   
  57.         android:visibility="gone"  
  58.         android:layout_width="wrap_content"   
  59.         android:layout_height="wrap_content"   
  60.         style="\?android:buttonStyleSmall" />  
  61. </LinearLayout>  

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

 

Xml代码
  1. <!-- 悬浮窗(FloatingService) permission -->  
  2. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值