仿360在Launcher画面显示内存使用率的浮窗(基础版)

来源:互联网 发布:熊本熊mac壁纸 编辑:程序博客网 时间:2024/05/29 08:13

转自:http://blog.csdn.net/lfdfhl/article/details/33394531

MainActivity如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. /** 
  11.  *  Demo描述: 
  12.  *  仿360在Launcher画面显示内存使用率的浮窗. 
  13.  *  当然这里只是简单地仿O(∩_∩)O 
  14.  *   
  15.  *  思路整理: 
  16.  *  1 涉及到大小两个浮窗.并且两个浮窗之间有逻辑联系.比如: 
  17.  *    显示小浮窗时不显示大浮窗.所以利用DriftingWindowManager 
  18.  *    来管理这两个浮窗 
  19.  *  2 各用一个类来封装和实现两个浮窗的操作 
  20.  *  3 以上两个类均继承自Layout 
  21.  *   
  22.  *  总的来讲该示例还是比较简单的 
  23.  *   
  24.  *  学习资料: 
  25.  *  1 http://blog.csdn.net/guolin_blog/article/details/8689140 
  26.  *  2 http://blog.csdn.net/feng88724/article/details/6362710 
  27.  *  3 http://blog.csdn.net/hudashi/article/details/6901118 
  28.  *  4 http://blog.csdn.net/hudashi/article/details/7060882 
  29.  *  5 http://blog.csdn.net/hudashi/article/details/7061240 
  30.  *    Thank you very much 
  31.  * 
  32.  */  
  33. public class MainActivity extends Activity {  
  34.     private Context mContext;  
  35.     private Button mStartButton;  
  36.     @Override  
  37.     protected void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.         init();  
  41.     }  
  42.       
  43.     private void init(){  
  44.         mContext=this;  
  45.         mStartButton=(Button) findViewById(R.id.button);  
  46.         mStartButton.setOnClickListener(new OnClickListener() {  
  47.             @Override  
  48.             public void onClick(View view) {  
  49.                 Intent intent=new Intent();  
  50.                 intent.setAction("dws");  
  51.                 mContext.startService(intent);  
  52.                 finish();  
  53.             }  
  54.         });  
  55.     }  
  56.   
  57.       
  58.   
  59. }  

DriftingBigWindow如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.LinearLayout;  
  9. /** 
  10.  *大浮窗 
  11.  * 
  12.  *大浮窗继承自LinearLayout 
  13.  *该大浮窗主要实现的功能 
  14.  *1 关闭大浮窗显示小浮窗 
  15.  *2 关闭所有浮窗且停止对于内存使用率的监控 
  16.  * 
  17.  *注意方法: 
  18.  *LayoutInflater.inflate(int resource, ViewGroup root)的第二参数 
  19.  *若设置了root,那么会把新生成的View连接到root,该方法的返回为root. 
  20.  *否未设置root,则返回的是新生成的View 
  21.  */  
  22. public class DriftingBigWindow extends LinearLayout {  
  23.     //整个大浮窗的宽和高  
  24.     public static int width=0;  
  25.     public static int height=0;  
  26.     private Context mContext;  
  27.     public DriftingBigWindow(Context context) {  
  28.         super(context);  
  29.         mContext=context;  
  30.         LayoutInflater layoutInflater=LayoutInflater.from(mContext);  
  31.         View bigWindowView=layoutInflater.inflate(R.layout.drifting_window_big, this);  
  32.         View driftingBigWindowRootView=bigWindowView.findViewById(R.id.driftingBigWindowRootView);  
  33.         //获取大浮窗整个布局的宽和高  
  34.         width=driftingBigWindowRootView.getLayoutParams().width;  
  35.         height=driftingBigWindowRootView.getLayoutParams().height;  
  36.         //停止服务且移除浮窗  
  37.         Button closeButton=(Button) bigWindowView.findViewById(R.id.closeButton);  
  38.         closeButton.setOnClickListener(new ClickListenerImpl());  
  39.         //显示小浮窗  
  40.         Button backButton=(Button) bigWindowView.findViewById(R.id.backButton);  
  41.         backButton.setOnClickListener(new ClickListenerImpl());  
  42.     }  
  43.       
  44.     private class ClickListenerImpl implements OnClickListener{  
  45.         @Override  
  46.         public void onClick(View view) {  
  47.             switch (view.getId()) {  
  48.             case R.id.closeButton:  
  49.                 Intent intent=new Intent();  
  50.                 intent.setAction("dws");  
  51.                 mContext.stopService(intent);  
  52.                 DriftingWindowManager.removeDriftingSmallWindow(mContext);  
  53.                 DriftingWindowManager.removeDriftingBiglWindow(mContext);  
  54.                 break;  
  55.             case R.id.backButton:  
  56.                 DriftingWindowManager.showDriftingSmallWindow(mContext);  
  57.                 DriftingWindowManager.removeDriftingBiglWindow(mContext);  
  58.                 break;  
  59.             default:  
  60.                 break;  
  61.             }  
  62.   
  63.         }  
  64.           
  65.     }  
  66.   
  67. }  

DriftingSmallWindow如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.WindowManager;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.TextView;  
  10. /** 
  11.  *小浮窗 
  12.  * 
  13.  *小浮窗继承自LinearLayout 
  14.  *该小浮窗主要实现的功能: 
  15.  *1 显示手机的内存使用率 
  16.  *2 在Home界面被随意移动位置 
  17.  *  所以重点是实现TouchListener,在Touch监听中 
  18.  *  不断调用mWindowManager.updateViewLayout() 
  19.  *  修改小浮窗在屏幕上的LayoutParams 
  20.  * 
  21.  *注意方法: 
  22.  *LayoutInflater.inflate(int resource, ViewGroup root)的第二参数 
  23.  *若设置了root,那么会把新生成的View连接到root,该方法的返回为root. 
  24.  *否未设置root,则返回的是新生成的View 
  25.  */  
  26. public class DriftingSmallWindow extends LinearLayout {  
  27.     //整个小浮窗的宽和高  
  28.     public static int width=0;  
  29.     public static int height=0;  
  30.     private float XInScreen_Down = 0;  
  31.     private float YInScreen_Down = 0;  
  32.     private float XInScreen_Move = 0;  
  33.     private float YInScreen_Move = 0;  
  34.     private float XInScreen_Up = 0;  
  35.     private float YInScreen_Up = 0;  
  36.     private float XInView_Down=0;  
  37.     private float YInView_Down=0;  
  38.     private Context mContext;  
  39.     private WindowManager mWindowManager;  
  40.     private static WindowManager.LayoutParams mWindowManagerLayoutParams;  
  41.       
  42.     public DriftingSmallWindow(Context context) {  
  43.         super(context);  
  44.         mContext=context;  
  45.         mWindowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
  46.         LayoutInflater layoutInflater = LayoutInflater.from(context);  
  47.         View smallWindowView=layoutInflater.inflate(R.layout.drifting_window_small, this);  
  48.         View driftingSmallWindowRootView=smallWindowView.findViewById(R.id.driftingSmallWindowRootView);  
  49.         TextView percentTextView=(TextView) smallWindowView.findViewById(R.id.percentTextView);  
  50.         //获取小浮窗整个布局的宽和高  
  51.         width=driftingSmallWindowRootView.getLayoutParams().width;  
  52.         height=driftingSmallWindowRootView.getLayoutParams().height;  
  53.         percentTextView.setText(Utils.getAvailMemoryPercent(context));  
  54.           
  55.         this.setOnTouchListener(new TouchListenerImpl());  
  56.         //this.setOnClickListener(new ClickListenerImpl());  
  57.     }  
  58.       
  59.     /** 
  60.      * 利用该方式监听点击事件不靠谱 
  61.      * 所以在MotionEvent.ACTION_UP中处理点击事件 
  62.      */  
  63.     private class ClickListenerImpl implements OnClickListener{  
  64.         @Override  
  65.         public void onClick(View view) {  
  66.               
  67.         }  
  68.           
  69.     }  
  70.       
  71.       
  72.     /** 
  73.      * 对于小浮窗Touch事件的监听 
  74.      * 注意在MotionEvent.ACTION_UP中处理点击事件 
  75.      */  
  76.     private class TouchListenerImpl implements OnTouchListener{  
  77.         @Override  
  78.         public boolean onTouch(View view, MotionEvent event) {  
  79.             int statusBarHeight=Utils.getStatusBarHeight(mContext);  
  80.             switch (event.getAction()) {  
  81.             case MotionEvent.ACTION_DOWN:  
  82.                 XInScreen_Down=event.getRawX();    
  83.                 YInScreen_Down=event.getRawY()-statusBarHeight;  
  84.                 XInView_Down=event.getX();  
  85.                 YInView_Down=event.getY();  
  86.                 break;  
  87.             case MotionEvent.ACTION_MOVE:  
  88.                  XInScreen_Move=event.getRawX();    
  89.                  YInScreen_Move=event.getRawY()-statusBarHeight;  
  90.                    
  91.                  updateWindowManagerLayoutParams();  
  92.                    
  93.                 break;  
  94.             case MotionEvent.ACTION_UP:  
  95.                  XInScreen_Up=event.getRawX();    
  96.                  YInScreen_Up=event.getRawY()-statusBarHeight;  
  97.                  if (XInScreen_Down==XInScreen_Up&&YInScreen_Down==YInScreen_Up) {  
  98.                      showDriftingBigWindow();  
  99.                 }  
  100.                 break;  
  101.   
  102.             default:  
  103.                 break;  
  104.             }  
  105.   
  106.             return true;  
  107.         }  
  108.           
  109.     };  
  110.       
  111.       
  112.       
  113.     /** 
  114.      * 保存小浮窗在Window中的布局参数 
  115.      */  
  116.     public static void saveWindowManagerLayoutParams(WindowManager.LayoutParams layoutParams){  
  117.         mWindowManagerLayoutParams=layoutParams;  
  118.     }  
  119.       
  120.       
  121.       
  122.     /** 
  123.      * 更新小浮窗在Window中的布局参数 
  124.      *  
  125.      * 注意事项: 
  126.      * X(Y)InScreen_Move表示触摸点离屏幕左上角的距离 
  127.      * X(Y)InView_Down表示触摸点离DriftingSmallWindow(小浮窗)自身左上角的距离. 
  128.      * 两者相减即得DriftingSmallWindow(小浮窗)左上角的坐标 
  129.      */  
  130.     private void updateWindowManagerLayoutParams(){  
  131.         mWindowManagerLayoutParams.x=(int) (XInScreen_Move-XInView_Down);  
  132.         mWindowManagerLayoutParams.y=(int) (YInScreen_Move-YInView_Down);  
  133.         mWindowManager.updateViewLayout(this, mWindowManagerLayoutParams);  
  134.     }  
  135.       
  136.     /** 
  137.      * 显示大浮窗且关闭小浮窗 
  138.      */  
  139.     private void showDriftingBigWindow(){  
  140.         DriftingWindowManager.showDriftingBiglWindow(mContext);  
  141.         DriftingWindowManager.removeDriftingSmallWindow(mContext);  
  142.     }  
  143.   
  144. }  

DriftingWindowManager如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.PixelFormat;  
  5. import android.view.Gravity;  
  6. import android.view.WindowManager;  
  7. import android.view.WindowManager.LayoutParams;  
  8. import android.widget.TextView;  
  9.   
  10. /** 
  11.  * 管理大小浮动窗口 
  12.  * 在该应用中主要包含了大小两个浮动窗口及其对应的操作 
  13.  * 比如显示,更新和移除等,所以就写了个DriftingWindowManager 
  14.  * 来实施这些操作. 
  15.  * 至于大小浮窗各自要做的操作则是在DriftingSmallWindow和DriftingBigWindow 
  16.  * 各个类中具体实施的. 
  17.  * 这个和平时其他的代码原理是一样的: 
  18.  * 比如在一个类A中使用了(类似于此处的浮窗显示,更新,移除)B和C的对象. 
  19.  * 但B和C对象的方法是在各自的类中实现的. 
  20.  */  
  21. public class DriftingWindowManager {  
  22.     private static WindowManager mWindowManager=null;  
  23.     private static DriftingSmallWindow mDriftingSmallWindow=null;  
  24.     private static DriftingBigWindow mDriftingBigWindow=null;  
  25.     //注意该LayoutParams属于android.view.WindowManager.LayoutParams  
  26.     private static LayoutParams mDriftingSmallWindowLayoutParams;  
  27.     private static LayoutParams mDriftingBigWindowLayoutParams;  
  28.       
  29.     /** 
  30.      * 显示小浮窗 
  31.      * 显示位置为屏幕中间右对齐 
  32.      */  
  33.     public static void showDriftingSmallWindow(Context context) {  
  34.         WindowManager windowManager = getWindowManager(context);  
  35.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  36.         int screenHeight = windowManager.getDefaultDisplay().getHeight();  
  37.         //new了一个DriftingSmallWindow对象,在后面会用WindowManager将  
  38.         //其添加到屏幕中  
  39.         mDriftingSmallWindow = new DriftingSmallWindow(context);  
  40.         if (mDriftingSmallWindowLayoutParams == null) {  
  41.             mDriftingSmallWindowLayoutParams = new LayoutParams();  
  42.             mDriftingSmallWindowLayoutParams.type = LayoutParams.TYPE_PHONE;  
  43.             mDriftingSmallWindowLayoutParams.format = PixelFormat.RGBA_8888;  
  44.             mDriftingSmallWindowLayoutParams.flags =   
  45.             LayoutParams.FLAG_NOT_TOUCH_MODAL| LayoutParams.FLAG_NOT_FOCUSABLE;  
  46.             mDriftingSmallWindowLayoutParams.gravity = Gravity.LEFT| Gravity.TOP;  
  47.             mDriftingSmallWindowLayoutParams.width = DriftingSmallWindow.width;  
  48.             mDriftingSmallWindowLayoutParams.height = DriftingSmallWindow.height;  
  49.             //使小浮窗在屏幕上垂直居中,水平靠右的位置显示  
  50.             mDriftingSmallWindowLayoutParams.x = screenWidth-DriftingSmallWindow.width;  
  51.             mDriftingSmallWindowLayoutParams.y = screenHeight / 2;  
  52.             System.out.println("DriftingSmallWindow.width="+DriftingSmallWindow.width);  
  53.             System.out.println("mDriftingSmallWindowLayoutParams.x="+mDriftingSmallWindowLayoutParams.x);  
  54.             System.out.println("mDriftingSmallWindowLayoutParams.y="+mDriftingSmallWindowLayoutParams.y);  
  55.         }  
  56.         //当显示小浮窗的时保存小浮窗的LayoutParams至该DriftingSmallWindow对象  
  57.         //因为每次移动小浮窗的时候需要修改该LayoutParams的参数值X和Y  
  58.         mDriftingSmallWindow.saveWindowManagerLayoutParams(mDriftingSmallWindowLayoutParams);  
  59.         mWindowManager.addView(mDriftingSmallWindow,mDriftingSmallWindowLayoutParams);  
  60.     }  
  61.       
  62.    
  63.       
  64.     /** 
  65.      * 更新小浮窗 
  66.      */  
  67.     public static void updateDriftingSmallWindow(Context context){  
  68.         if(mDriftingSmallWindow!=null){  
  69.             TextView percentTextView=(TextView) mDriftingSmallWindow.findViewById(R.id.percentTextView);  
  70.             percentTextView.setText(Utils.getAvailMemoryPercent(context));  
  71.         }  
  72.     }  
  73.       
  74.       
  75.     /** 
  76.      * 移除小浮窗 
  77.      */  
  78.     public static void removeDriftingSmallWindow(Context context){  
  79.         mWindowManager=getWindowManager(context);  
  80.         if(mWindowManager!=null&&mDriftingSmallWindow!=null){  
  81.             mWindowManager.removeView(mDriftingSmallWindow);  
  82.             mDriftingSmallWindow=null;  
  83.         }  
  84.     }  
  85.       
  86.       
  87.     /** 
  88.      * 显示大浮窗 
  89.      * 显示位置为屏幕中间 
  90.      *  
  91.      * 注意细节问题 
  92.      * 如下写法,有偏差,显示效果并不好 
  93.      * mDriftingBigWindowLayoutParams.x = screenWidth / 2; 
  94.      * mDriftingBigWindowLayoutParams.y = screenHeight / 2; 
  95.      * 给人的感觉是大浮窗并没有在屏幕中间位置. 
  96.      * 因为这个mDriftingBigWindowLayoutParams.x(y)指的是大浮窗 
  97.      * 在屏幕上显示的x(y)的开始坐标值,即从哪个坐标开始摆放大浮窗. 
  98.      * 极端地说如果大浮窗就沙子那么大,那么这么做就没有问题,因为大浮窗 
  99.      * 本身就没有什么宽和高. 
  100.      * 但在实际中我们还要考虑到控件本身(此处的大浮窗)的长和宽,做到 
  101.      * 真的居中显示 
  102.      * 所以应该这么写: 
  103.      * mDriftingBigWindowLayoutParams.x = screenWidth / 2- DriftingBigWindow.width / 2; 
  104.      * mDriftingBigWindowLayoutParams.y = screenHeight / 2- DriftingBigWindow.height / 2; 
  105.      * 类似的问题在小浮窗的拖动过程中也有 
  106.      */  
  107.     public static void showDriftingBiglWindow(Context context) {  
  108.         WindowManager windowManager = getWindowManager(context);  
  109.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  110.         int screenHeight = windowManager.getDefaultDisplay().getHeight();  
  111.         mDriftingBigWindow = new DriftingBigWindow(context);  
  112.         if (mDriftingBigWindowLayoutParams == null) {  
  113.             mDriftingBigWindowLayoutParams = new LayoutParams();  
  114.             mDriftingBigWindowLayoutParams.x = screenWidth / 2- DriftingBigWindow.width / 2;  
  115.             mDriftingBigWindowLayoutParams.y = screenHeight / 2- DriftingBigWindow.height / 2;  
  116.             mDriftingBigWindowLayoutParams.type = LayoutParams.TYPE_PHONE;  
  117.             mDriftingBigWindowLayoutParams.format = PixelFormat.RGBA_8888;  
  118.             mDriftingBigWindowLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;  
  119.             mDriftingBigWindowLayoutParams.width = DriftingBigWindow.width;  
  120.             mDriftingBigWindowLayoutParams.height = DriftingBigWindow.height;  
  121.         }  
  122.         windowManager.addView(mDriftingBigWindow,mDriftingBigWindowLayoutParams);  
  123.     }  
  124.       
  125.       
  126.     /** 
  127.      * 移除大浮窗 
  128.      */  
  129.     public static void removeDriftingBiglWindow(Context context){  
  130.         mWindowManager=getWindowManager(context);  
  131.         if(mWindowManager!=null&&mDriftingBigWindow!=null){  
  132.             mWindowManager.removeView(mDriftingBigWindow);  
  133.             mDriftingBigWindow=null;  
  134.         }  
  135.     }  
  136.      
  137.       
  138.     /** 
  139.      * 是否有浮窗在Launcher上显示 
  140.      */  
  141.     public static boolean isDriftingWindowShowing(){  
  142.         if (mDriftingSmallWindow!=null||mDriftingBigWindow!=null) {  
  143.             return true;  
  144.         } else {  
  145.            return false;  
  146.         }  
  147.     }  
  148.       
  149.       
  150.     /** 
  151.      * 获取WindowManager 
  152.      */  
  153.     private static WindowManager getWindowManager(Context context){  
  154.         if (mWindowManager==null) {  
  155.             mWindowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
  156.         }  
  157.         return mWindowManager;  
  158.     }  
  159.       
  160. }  

DriftingWindowService如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5. import android.app.Service;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.os.Handler;  
  9. import android.os.IBinder;  
  10. /** 
  11.  * 利用该服务进行定时任务 
  12.  * 
  13.  */  
  14. public class DriftingWindowService extends Service {  
  15.     private Timer mTimer;  
  16.     private TimerTask mTimerTask;  
  17.     private Context mContext;  
  18.     private Handler mHandler;  
  19.     @Override  
  20.     public void onCreate() {  
  21.         super.onCreate();  
  22.     }  
  23.       
  24.     @Override  
  25.     public void onStart(Intent intent, int startId) {  
  26.         super.onStart(intent, startId);  
  27.         mContext=this;  
  28.         mHandler=new Handler();  
  29.         mTimer=new Timer();  
  30.         mTimerTask=new TimerTaskSubclass();  
  31.         //开启定时的任务  
  32.         mTimer.schedule(mTimerTask, 100500);  
  33.     }  
  34.       
  35.     @Override  
  36.     public IBinder onBind(Intent arg0) {  
  37.         return null;  
  38.     }  
  39.       
  40.     @Override  
  41.     public void onDestroy() {  
  42.         super.onDestroy();  
  43.         if (mTimer!=null) {  
  44.             mTimer.cancel();  
  45.         }  
  46.     }  
  47.       
  48.       
  49.     private class TimerTaskSubclass extends TimerTask{  
  50.         @Override  
  51.         public void run() {  
  52.             //当前是Launcher,则显示小浮窗  
  53.             if (Utils.currentIsLauncher(mContext)&&!DriftingWindowManager.isDriftingWindowShowing()) {  
  54.                 mHandler.post(new Runnable() {  
  55.                     @Override  
  56.                     public void run() {  
  57.                         DriftingWindowManager.showDriftingSmallWindow(mContext);  
  58.                     }  
  59.                 });  
  60.             }  
  61.               
  62.             //当前不是Launcher且有浮窗显示,则移除浮窗  
  63.             if(!Utils.currentIsLauncher(mContext)&&DriftingWindowManager.isDriftingWindowShowing()){  
  64.                 mHandler.post(new Runnable() {  
  65.                     @Override  
  66.                     public void run() {  
  67.                         DriftingWindowManager.removeDriftingSmallWindow(mContext);  
  68.                         DriftingWindowManager.removeDriftingBiglWindow(mContext);  
  69.                     }  
  70.                 });  
  71.             }  
  72.               
  73.             //当前是Launcher,则更新内存使用率  
  74.             if(Utils.currentIsLauncher(mContext)){  
  75.                 mHandler.post(new Runnable() {  
  76.                     @Override  
  77.                     public void run() {  
  78.                         DriftingWindowManager.updateDriftingSmallWindow(mContext);  
  79.                     }  
  80.                 });  
  81.             }  
  82.         }  
  83.           
  84.     }  
  85.   
  86. }  

Utils如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.cc;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.lang.reflect.Field;  
  7. import java.util.List;  
  8. import android.app.ActivityManager;  
  9. import android.app.ActivityManager.MemoryInfo;  
  10. import android.app.ActivityManager.RunningTaskInfo;  
  11. import android.content.ComponentName;  
  12. import android.content.Context;  
  13.   
  14. public class Utils {  
  15.     /** 
  16.      * 获取设备状态栏高度 
  17.      */  
  18.     public static int getStatusBarHeight(Context context) {  
  19.         int statusBarHeight = 0;  
  20.         try {  
  21.             Class clazz = Class.forName("com.android.internal.R$dimen");  
  22.             Object object = clazz.newInstance();  
  23.             Field field = clazz.getField("status_bar_height");  
  24.             // 反射出该对象中status_bar_height字段所对应的在R文件的id值  
  25.             // 该id值由系统工具自动生成,文档描述如下:  
  26.             // The desired resource identifier, as generated by the aapt tool.  
  27.             int id = Integer.parseInt(field.get(object).toString());  
  28.             // 依据id值获取到状态栏的高度,单位为像素  
  29.             statusBarHeight = context.getResources().getDimensionPixelSize(id);  
  30.         } catch (Exception e) {  
  31.         }  
  32.         return statusBarHeight;  
  33.     }  
  34.       
  35.       
  36.       
  37.     /** 
  38.      * 判断设备当前是否停留在Launcher 
  39.      */  
  40.     public static boolean currentIsLauncher(Context context){  
  41.         boolean isLauncher=false;  
  42.         String topActivityName=getTopActivityName(context);  
  43.         if (topActivityName!=null&&topActivityName.startsWith("HomeActivity")) {  
  44.             isLauncher=true;  
  45.         }  
  46.         return isLauncher;  
  47.     }  
  48.       
  49.       
  50.       
  51.     /** 
  52.      * 获取栈顶Activity名称 
  53.      */  
  54.     public static String getTopActivityName(Context context) {  
  55.         String topActivityName = null;  
  56.         ActivityManager activityManager =  
  57.         (ActivityManager)(context.getSystemService(android.content.Context.ACTIVITY_SERVICE));  
  58.         List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);  
  59.         if (runningTaskInfos != null) {  
  60.             ComponentName f = runningTaskInfos.get(0).topActivity;  
  61.             String topActivityClassName = f.getClassName();  
  62.             String temp[] = topActivityClassName.split("\\.");  
  63.             // 栈顶Activity的名称  
  64.             topActivityName = temp[temp.length - 1];  
  65.         }  
  66.         return topActivityName;  
  67.     }  
  68.       
  69.       
  70.       
  71.     /** 
  72.      * 获取当前内存的可用率 
  73.      */  
  74.     public static String getAvailMemoryPercent(Context context){  
  75.         String info=null;  
  76.         long availMemory=getAvailMemory(context);  
  77.         long totalMemory=getTotalMemory();  
  78.         float percent=(availMemory*100/totalMemory);  
  79.         info=percent+"%";  
  80.         return info;  
  81.     }  
  82.       
  83.       
  84.       
  85.     /** 
  86.      * 获取内存总大小 
  87.      */  
  88.     public static long getTotalMemory() {  
  89.         // 系统的内存信息文件  
  90.         String filePath = "/proc/meminfo";  
  91.         String lineString;  
  92.         String[] stringArray;  
  93.         long totalMemory = 0;  
  94.         try {  
  95.             FileReader fileReader = new FileReader(filePath);  
  96.             BufferedReader bufferedReader = new BufferedReader(fileReader,1024 * 8);  
  97.             // 读取meminfo第一行,获取系统总内存大小  
  98.             lineString = bufferedReader.readLine();  
  99.             // 按照空格拆分  
  100.             stringArray = lineString.split("\\s+");  
  101.             // 获得系统总内存,单位KB  
  102.             totalMemory = Integer.valueOf(stringArray[1]).intValue();  
  103.             bufferedReader.close();  
  104.         } catch (IOException e) {  
  105.         }  
  106.         return totalMemory / 1024;  
  107.     }  
  108.   
  109.       
  110.       
  111.     /** 
  112.      * 获取可用内存大小 
  113.      */  
  114.     public static long getAvailMemory(Context context) {  
  115.         ActivityManager activityManager =   
  116.         (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
  117.         MemoryInfo memoryInfo = new MemoryInfo();  
  118.         activityManager.getMemoryInfo(memoryInfo);  
  119.         return memoryInfo.availMem / (1024 * 1024);  
  120.     }  
  121.   
  122. }  

main.xml如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.    >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="开启浮动窗口"  
  12.         android:layout_centerInParent="true"  
  13.     />  
  14.   
  15. </RelativeLayout>  

drifting_window_big.xml如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/driftingBigWindowRootView"  
  4.     android:layout_width="130dip"  
  5.     android:layout_height="130dip"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/closeButton"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:text="关闭所有浮窗" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/backButton"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_gravity="center_horizontal"  
  20.         android:text="返回到小浮窗" />  
  21.   
  22. </LinearLayout>  

drifting_window_small.xml如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     android:id="@+id/driftingSmallWindowRootView"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:layout_width="45dip"  
  6.     android:layout_height="30dip"  
  7.     android:background="#00cc00"  
  8.     android:orientation="horizontal" >  
  9.   
  10.     <TextView  
  11.         android:id="@+id/percentTextView"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:gravity="center"    
  15.         android:textColor="#ff0000"  
  16.         android:layout_gravity="center_vertical"  
  17.          />  
  18.   
  19. </LinearLayout>  

AndroidManifest.xml如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cc.cc"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="10"  
  9.         android:targetSdkVersion="10" />  
  10.       
  11.      <!-- 注意权限 -->  
  12.     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />    
  13.     <uses-permission android:name="android.permission.GET_TASKS"/>    
  14.   
  15.     <application  
  16.         android:allowBackup="true"  
  17.         android:icon="@drawable/ic_launcher"  
  18.         android:label="@string/app_name"  
  19.         android:theme="@style/AppTheme" >  
  20.         <activity  
  21.             android:name="cc.cc.MainActivity"  
  22.             android:label="@string/app_name" >  
  23.             <intent-filter>  
  24.                 <action android:name="android.intent.action.MAIN" />  
  25.   
  26.                 <category android:name="android.intent.category.LAUNCHER" />  
  27.             </intent-filter>  
  28.         </activity>  
  29.           
  30.         <!-- 注册服务 -->  
  31.         <service android:name="cc.cc.DriftingWindowService">  
  32.             <intent-filter >  
  33.                 <action android:name="dws"/>  
  34.             </intent-filter>  
  35.         </service>  
  36.     </application>  
  37.   
  38. </manifest>  


0 0