

首先是一個小的懸浮窗顯示的是當前使用了百分之多少的內存,點擊一下小懸浮窗,就會彈出一個大的懸浮窗,可以一鍵加速。好,我們現在就來模擬實現一下類似的效果。
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>
