欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿360桌面手機(jī)衛(wèi)士懸浮窗效果

 更新時(shí)間:2018年05月10日 12:10:06   作者:guolin  
這篇文章主要介紹了Android仿360手機(jī)衛(wèi)士懸浮窗效果的桌面實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

大家好,今天給大家?guī)?lái)一個(gè)仿360手機(jī)衛(wèi)士懸浮窗效果的教程,在開始之前請(qǐng)?jiān)试S我先說(shuō)幾句不相干的話。

不知不覺(jué)我發(fā)現(xiàn)自己接觸Android已有近三個(gè)年頭了,期間各種的成長(zhǎng)少不了各位高手的幫助,總是有很多高手喜歡把自己的經(jīng)驗(yàn)寫在網(wǎng)上,供大家來(lái)學(xué)習(xí),我也是從中受惠了很多,在此我深表感謝??墒俏野l(fā)現(xiàn)我卻從來(lái)沒(méi)有將自己平時(shí)的一些心得拿出來(lái)與大家分享,共同學(xué)習(xí),太沒(méi)有奉獻(xiàn)精神了。于是我痛定思痛,決定從今天開始寫博客,希望可以指點(diǎn)在我后面的開發(fā)者,更快地進(jìn)入Android開發(fā)者的行列當(dāng)中。

好了,廢話就說(shuō)這么多,下面開始進(jìn)入今天的主題吧。

360手機(jī)衛(wèi)士我相信大家都知道,好多人手機(jī)上都會(huì)裝這一款軟件,那么我們對(duì)它的一個(gè)桌面懸浮窗效果想必都不會(huì)陌生。請(qǐng)看下圖:

           

首先是一個(gè)小的懸浮窗顯示的是當(dāng)前使用了百分之多少的內(nèi)存,點(diǎn)擊一下小懸浮窗,就會(huì)彈出一個(gè)大的懸浮窗,可以一鍵加速。好,我們現(xiàn)在就來(lái)模擬實(shí)現(xiàn)一下類似的效果。

先談一下基本的實(shí)現(xiàn)原理,這種桌面懸浮窗的效果很類似與Widget,但是它比Widget要靈活的多。主要是通過(guò)WindowManager這個(gè)類來(lái)實(shí)現(xiàn)的,調(diào)用這個(gè)類的addView方法用于添加一個(gè)懸浮窗,updateViewLayout方法用于更新懸浮窗的參數(shù),removeView用于移除懸浮窗。其中懸浮窗的參數(shù)有必要詳細(xì)說(shuō)明一下。

WindowManager.LayoutParams這個(gè)類用于提供懸浮窗所需的參數(shù),其中有幾個(gè)經(jīng)常會(huì)用到的變量:

type值用于確定懸浮窗的類型,一般設(shè)為2002,表示在所有應(yīng)用程序之上,但在狀態(tài)欄之下。

flags值用于確定懸浮窗的行為,比如說(shuō)不可聚焦,非模態(tài)對(duì)話框等等,屬性非常多,大家可以查看文檔。

gravity值用于確定懸浮窗的對(duì)齊方式,一般設(shè)為左上角對(duì)齊,這樣當(dāng)拖動(dòng)懸浮窗的時(shí)候方便計(jì)算坐標(biāo)。

x值用于確定懸浮窗的位置,如果要橫向移動(dòng)懸浮窗,就需要改變這個(gè)值。

y值用于確定懸浮窗的位置,如果要縱向移動(dòng)懸浮窗,就需要改變這個(gè)值。

width值用于指定懸浮窗的寬度。

height值用于指定懸浮窗的高度。

創(chuàng)建懸浮窗這種窗體需要向用戶申請(qǐng)權(quán)限才可以的,因此還需要在AndroidManifest.xml中加入

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

原理介紹完了,下面我們開始用代碼實(shí)現(xiàn)。首先在Eclipse中新建一個(gè)Android項(xiàng)目,項(xiàng)目名就叫做360FloatWindowDemo。然后寫一下布局文件,布局文件非常簡(jiǎn)單,只有一個(gè)按鈕,打開或新建activity_main.xml,

加入如下代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 tools:context=".MainActivity" > 
 <Button 
 android:id="@+id/start_float_window" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="Start Float Window" > 
 </Button> 
</RelativeLayout> 

然后再新建一個(gè)名為float_window_small.xml的布局文件,用于做為小懸浮窗的布局,

在其中加入如下代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/small_window_layout" 
 android:layout_width="60dip" 
 android:layout_height="25dip" 
 android:background="@drawable/bg_small" 
 > 
 <TextView 
 android:id="@+id/percent" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:gravity="center" 
 android:textColor="#ffffff" 
 /> 
</LinearLayout> 

再新建一個(gè)名為float_window_big.xml的布局文件,用于做為大懸浮窗的布局,

在其中加入如下代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/big_window_layout" 
 android:layout_width="200dip" 
 android:layout_height="100dip" 
 android:background="@drawable/bg_big" 
 android:orientation="vertical" 
 > 
 <Button 
 android:id="@+id/close" 
 android:layout_width="100dip" 
 android:layout_height="40dip" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginTop="12dip" 
 android:text="關(guān)閉懸浮窗" 
 /> 
 <Button 
 android:id="@+id/back" 
 android:layout_width="100dip" 
 android:layout_height="40dip" 
 android:layout_gravity="center_horizontal" 
 android:text="返回" 
 /> 
</LinearLayout> 

兩個(gè)懸浮窗布局文件中用到的圖片資源,大家可以隨便找點(diǎn)圖片來(lái)代替,同時(shí)我會(huì)給出源碼,大家也可以從源碼中取出。

然后打開或創(chuàng)建MainActivity,這是項(xiàng)目的主界面

在里面加入如下代碼:

public class MainActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 Button startFloatWindow = (Button) findViewById(R.id.start_float_window); 
 startFloatWindow.setOnClickListener(new OnClickListener() { 
 @Override 
 public void onClick(View arg0) { 
 Intent intent = new Intent(MainActivity.this, FloatWindowService.class); 
 startService(intent); 
 finish(); 
 } 
 }); 
 } 
}

這里可以看到,MainActivity的代碼非窗簡(jiǎn)單,就是對(duì)開啟懸浮窗的按鈕注冊(cè)了一個(gè)點(diǎn)擊事件,用于打開一個(gè)服務(wù),然后關(guān)閉當(dāng)前Activity。創(chuàng)建懸浮窗的邏輯都交給服務(wù)去做了。好,現(xiàn)在我們來(lái)創(chuàng)建這個(gè)服務(wù)。新建一個(gè)名為FloatWindowService的類,這個(gè)類繼承自Service

在里面加入如下代碼:

public class FloatWindowService extends Service { 
 
 /** 
 * 用于在線程中創(chuàng)建或移除懸浮窗。 
 */ 
 private Handler handler = new Handler(); 
 
 /** 
 * 定時(shí)器,定時(shí)進(jìn)行檢測(cè)當(dāng)前應(yīng)該創(chuàng)建還是移除懸浮窗。 
 */ 
 private Timer timer; 
 
 @Override 
 public IBinder onBind(Intent intent) { 
 return null; 
 } 
 
 @Override 
 public int onStartCommand(Intent intent, int flags, int startId) { 
 // 開啟定時(shí)器,每隔0.5秒刷新一次 
 if (timer == null) { 
 timer = new Timer(); 
 timer.scheduleAtFixedRate(new RefreshTask(), 0, 500); 
 } 
 return super.onStartCommand(intent, flags, startId); 
 } 
 
 @Override 
 public void onDestroy() { 
 super.onDestroy(); 
 // Service被終止的同時(shí)也停止定時(shí)器繼續(xù)運(yùn)行 
 timer.cancel(); 
 timer = null; 
 } 
 
 class RefreshTask extends TimerTask { 
 
 @Override 
 public void run() { 
 // 當(dāng)前界面是桌面,且沒(méi)有懸浮窗顯示,則創(chuàng)建懸浮窗。 
 if (isHome() && !MyWindowManager.isWindowShowing()) { 
 handler.post(new Runnable() { 
 @Override 
 public void run() { 
 MyWindowManager.createSmallWindow(getApplicationContext()); 
 } 
 }); 
 } 
 // 當(dāng)前界面不是桌面,且有懸浮窗顯示,則移除懸浮窗。 
 else if (!isHome() && MyWindowManager.isWindowShowing()) { 
 handler.post(new Runnable() { 
 @Override 
 public void run() { 
 MyWindowManager.removeSmallWindow(getApplicationContext()); 
 MyWindowManager.removeBigWindow(getApplicationContext()); 
 } 
 }); 
 } 
 // 當(dāng)前界面是桌面,且有懸浮窗顯示,則更新內(nèi)存數(shù)據(jù)。 
 else if (isHome() && MyWindowManager.isWindowShowing()) { 
 handler.post(new Runnable() { 
 @Override 
 public void run() { 
 MyWindowManager.updateUsedPercent(getApplicationContext()); 
 } 
 }); 
 } 
 } 
 
 } 
 
 /** 
 * 判斷當(dāng)前界面是否是桌面 
 */ 
 private boolean isHome() { 
 ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
 List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1); 
 return getHomes().contains(rti.get(0).topActivity.getPackageName()); 
 } 
 
 /** 
 * 獲得屬于桌面的應(yīng)用的應(yīng)用包名稱 
 * 
 * @return 返回包含所有包名的字符串列表 
 */ 
 private List<String> getHomes() { 
 List<String> names = new ArrayList<String>(); 
 PackageManager packageManager = this.getPackageManager(); 
 Intent intent = new Intent(Intent.ACTION_MAIN); 
 intent.addCategory(Intent.CATEGORY_HOME); 
 List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, 
 PackageManager.MATCH_DEFAULT_ONLY); 
 for (ResolveInfo ri : resolveInfo) { 
 names.add(ri.activityInfo.packageName); 
 } 
 return names; 
 } 
} 

FloatWindowService的onStartCommand方法中開啟了一個(gè)定時(shí)器,每隔500毫秒就會(huì)執(zhí)行RefreshTask。在RefreshTask當(dāng)中,要進(jìn)行判斷,如果手機(jī)當(dāng)前是在桌面的話,就應(yīng)該顯示懸浮窗,如果手機(jī)打開了某一個(gè)應(yīng)用程序,就應(yīng)該移除懸浮窗,如果手機(jī)在桌面的話,還應(yīng)該更新內(nèi)存使用百分比的數(shù)據(jù)。而當(dāng)FloatWindowService被銷毀的時(shí)候,應(yīng)該將定時(shí)器停止,否則它還會(huì)一直運(yùn)行。

從上面的代碼我們也可以看出,創(chuàng)建和移除懸浮窗,以及更新懸浮窗內(nèi)的數(shù)據(jù),都是由MyWindowManager這個(gè)類來(lái)管理的,比起直接把這些代碼寫在Activity或Service當(dāng)中,使用一個(gè)專門的工具類來(lái)管理要好的多。不過(guò)要想創(chuàng)建懸浮窗,還是先要把懸浮窗的View寫出來(lái)。

新建一個(gè)名叫FloatWindowSmallView的類,繼承自LinearLayout。新建一個(gè)名叫FloatWindowBigView的類,也繼承自LinearLayout。

在FloatWindowSmallView中加入如下代碼:

public class FloatWindowSmallView extends LinearLayout { 
 
 /** 
 * 記錄小懸浮窗的寬度 
 */ 
 public static int viewWidth; 
 
 /** 
 * 記錄小懸浮窗的高度 
 */ 
 public static int viewHeight; 
 
 /** 
 * 記錄系統(tǒng)狀態(tài)欄的高度 
 */ 
 private static int statusBarHeight; 
 
 /** 
 * 用于更新小懸浮窗的位置 
 */ 
 private WindowManager windowManager; 
 
 /** 
 * 小懸浮窗的參數(shù) 
 */ 
 private WindowManager.LayoutParams mParams; 
 
 /** 
 * 記錄當(dāng)前手指位置在屏幕上的橫坐標(biāo)值 
 */ 
 private float xInScreen; 
 
 /** 
 * 記錄當(dāng)前手指位置在屏幕上的縱坐標(biāo)值 
 */ 
 private float yInScreen; 
 
 /** 
 * 記錄手指按下時(shí)在屏幕上的橫坐標(biāo)的值 
 */ 
 private float xDownInScreen; 
 
 /** 
 * 記錄手指按下時(shí)在屏幕上的縱坐標(biāo)的值 
 */ 
 private float yDownInScreen; 
 
 /** 
 * 記錄手指按下時(shí)在小懸浮窗的View上的橫坐標(biāo)的值 
 */ 
 private float xInView; 
 
 /** 
 * 記錄手指按下時(shí)在小懸浮窗的View上的縱坐標(biāo)的值 
 */ 
 private float yInView; 
 
 public FloatWindowSmallView(Context context) { 
 super(context); 
 windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
 LayoutInflater.from(context).inflate(R.layout.float_window_small, this); 
 View view = findViewById(R.id.small_window_layout); 
 viewWidth = view.getLayoutParams().width; 
 viewHeight = view.getLayoutParams().height; 
 TextView percentView = (TextView) findViewById(R.id.percent); 
 percentView.setText(MyWindowManager.getUsedPercentValue(context)); 
 } 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
 switch (event.getAction()) { 
 case MotionEvent.ACTION_DOWN: 
 // 手指按下時(shí)記錄必要數(shù)據(jù),縱坐標(biāo)的值都需要減去狀態(tài)欄高度 
 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(); 
 // 手指移動(dòng)的時(shí)候更新小懸浮窗的位置 
 updateViewPosition(); 
 break; 
 case MotionEvent.ACTION_UP: 
 // 如果手指離開屏幕時(shí),xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,則視為觸發(fā)了單擊事件。 
 if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) { 
 openBigWindow(); 
 } 
 break; 
 default: 
 break; 
 } 
 return true; 
 } 
 
 /** 
 * 將小懸浮窗的參數(shù)傳入,用于更新小懸浮窗的位置。 
 * 
 * @param params 
 * 小懸浮窗的參數(shù) 
 */ 
 public void setParams(WindowManager.LayoutParams params) { 
 mParams = params; 
 } 
 
 /** 
 * 更新小懸浮窗在屏幕中的位置。 
 */ 
 private void updateViewPosition() { 
 mParams.x = (int) (xInScreen - xInView); 
 mParams.y = (int) (yInScreen - yInView); 
 windowManager.updateViewLayout(this, mParams); 
 } 
 
 /** 
 * 打開大懸浮窗,同時(shí)關(guān)閉小懸浮窗。 
 */ 
 private void openBigWindow() { 
 MyWindowManager.createBigWindow(getContext()); 
 MyWindowManager.removeSmallWindow(getContext()); 
 } 
 
 /** 
 * 用于獲取狀態(tài)欄的高度。 
 * 
 * @return 返回狀態(tài)欄高度的像素值。 
 */ 
 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 = (Integer) field.get(o); 
 statusBarHeight = getResources().getDimensionPixelSize(x); 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 return statusBarHeight; 
 } 

其中,對(duì)這個(gè)View的onTouchEvent事件進(jìn)行了重寫,用于實(shí)現(xiàn)拖動(dòng)和點(diǎn)擊的效果。如果發(fā)現(xiàn)用戶觸發(fā)了ACTION_DOWN事件,會(huì)記錄按下時(shí)的坐標(biāo)等數(shù)據(jù)。如果發(fā)現(xiàn)用戶觸發(fā)了ACTION_MOVE事件,則根據(jù)當(dāng)前移動(dòng)的坐標(biāo)更新懸浮窗在屏幕中的位置。如果發(fā)現(xiàn)用戶觸發(fā)了ACTION_UP事件,會(huì)和ACTION_DOWN中記下的坐標(biāo)對(duì)比,如果發(fā)現(xiàn)是相同的,則視為用戶對(duì)懸浮窗進(jìn)行了點(diǎn)擊。點(diǎn)擊小懸浮窗則打開大懸浮窗,然后我們來(lái)實(shí)現(xiàn)大懸浮窗的View。

在FloatWindowBigView中加入如下代碼:

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_window_big, this); 
 View view = findViewById(R.id.big_window_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) { 
 // 點(diǎn)擊關(guān)閉懸浮窗的時(shí)候,移除所有懸浮窗,并停止Service 
 MyWindowManager.removeBigWindow(context); 
 MyWindowManager.removeSmallWindow(context); 
 Intent intent = new Intent(getContext(), FloatWindowService.class); 
 context.stopService(intent); 
 } 
 }); 
 back.setOnClickListener(new OnClickListener() { 
 @Override 
 public void onClick(View v) { 
 // 點(diǎn)擊返回的時(shí)候,移除大懸浮窗,創(chuàng)建小懸浮窗 
 MyWindowManager.removeBigWindow(context); 
 MyWindowManager.createSmallWindow(context); 
 } 
 }); 
 } 
}

比起FloatWindowSmallView,F(xiàn)loatWindowBigView要簡(jiǎn)單的多,其中只有兩個(gè)按鈕,點(diǎn)擊close按鈕,將懸浮窗全部移除,并將Service終止。單擊back按鈕則移除大懸浮窗,重新創(chuàng)建小懸浮窗。

現(xiàn)在兩個(gè)懸浮窗的View都已經(jīng)寫好了,我們來(lái)創(chuàng)建MyWindowManager,

代碼如下:

public class MyWindowManager { 
 
 /** 
 * 小懸浮窗View的實(shí)例 
 */ 
 private static FloatWindowSmallView smallWindow; 
 
 /** 
 * 大懸浮窗View的實(shí)例 
 */ 
 private static FloatWindowBigView bigWindow; 
 
 /** 
 * 小懸浮窗View的參數(shù) 
 */ 
 private static LayoutParams smallWindowParams; 
 
 /** 
 * 大懸浮窗View的參數(shù) 
 */ 
 private static LayoutParams bigWindowParams; 
 
 /** 
 * 用于控制在屏幕上添加或移除懸浮窗 
 */ 
 private static WindowManager mWindowManager; 
 
 /** 
 * 用于獲取手機(jī)可用內(nèi)存 
 */ 
 private static ActivityManager mActivityManager; 
 
 /** 
 * 創(chuàng)建一個(gè)小懸浮窗。初始位置為屏幕的右部中間位置。 
 * 
 * @param context 
 * 必須為應(yīng)用程序的Context. 
 */ 
 public static void createSmallWindow(Context context) { 
 WindowManager windowManager = getWindowManager(context); 
 int screenWidth = windowManager.getDefaultDisplay().getWidth(); 
 int screenHeight = windowManager.getDefaultDisplay().getHeight(); 
 if (smallWindow == null) { 
 smallWindow = new FloatWindowSmallView(context); 
 if (smallWindowParams == null) { 
 smallWindowParams = new LayoutParams(); 
 smallWindowParams.type = LayoutParams.TYPE_PHONE; 
 smallWindowParams.format = PixelFormat.RGBA_8888; 
 smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
 | LayoutParams.FLAG_NOT_FOCUSABLE; 
 smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP; 
 smallWindowParams.width = FloatWindowSmallView.viewWidth; 
 smallWindowParams.height = FloatWindowSmallView.viewHeight; 
 smallWindowParams.x = screenWidth; 
 smallWindowParams.y = screenHeight / 2; 
 } 
 smallWindow.setParams(smallWindowParams); 
 windowManager.addView(smallWindow, smallWindowParams); 
 } 
 } 
 
 /** 
 * 將小懸浮窗從屏幕上移除。 
 * 
 * @param context 
 * 必須為應(yīng)用程序的Context. 
 */ 
 public static void removeSmallWindow(Context context) { 
 if (smallWindow != null) { 
 WindowManager windowManager = getWindowManager(context); 
 windowManager.removeView(smallWindow); 
 smallWindow = null; 
 } 
 } 
 
 /** 
 * 創(chuàng)建一個(gè)大懸浮窗。位置為屏幕正中間。 
 * 
 * @param context 
 * 必須為應(yīng)用程序的Context. 
 */ 
 public static void createBigWindow(Context context) { 
 WindowManager windowManager = getWindowManager(context); 
 int screenWidth = windowManager.getDefaultDisplay().getWidth(); 
 int screenHeight = windowManager.getDefaultDisplay().getHeight(); 
 if (bigWindow == null) { 
 bigWindow = new FloatWindowBigView(context); 
 if (bigWindowParams == null) { 
 bigWindowParams = new LayoutParams(); 
 bigWindowParams.x = screenWidth / 2 - FloatWindowBigView.viewWidth / 2; 
 bigWindowParams.y = screenHeight / 2 - FloatWindowBigView.viewHeight / 2; 
 bigWindowParams.type = LayoutParams.TYPE_PHONE; 
 bigWindowParams.format = PixelFormat.RGBA_8888; 
 bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP; 
 bigWindowParams.width = FloatWindowBigView.viewWidth; 
 bigWindowParams.height = FloatWindowBigView.viewHeight; 
 } 
 windowManager.addView(bigWindow, bigWindowParams); 
 } 
 } 
 
 /** 
 * 將大懸浮窗從屏幕上移除。 
 * 
 * @param context 
 * 必須為應(yīng)用程序的Context. 
 */ 
 public static void removeBigWindow(Context context) { 
 if (bigWindow != null) { 
 WindowManager windowManager = getWindowManager(context); 
 windowManager.removeView(bigWindow); 
 bigWindow = null; 
 } 
 } 
 
 /** 
 * 更新小懸浮窗的TextView上的數(shù)據(jù),顯示內(nèi)存使用的百分比。 
 * 
 * @param context 
 * 可傳入應(yīng)用程序上下文。 
 */ 
 public static void updateUsedPercent(Context context) { 
 if (smallWindow != null) { 
 TextView percentView = (TextView) smallWindow.findViewById(R.id.percent); 
 percentView.setText(getUsedPercentValue(context)); 
 } 
 } 
 
 /** 
 * 是否有懸浮窗(包括小懸浮窗和大懸浮窗)顯示在屏幕上。 
 * 
 * @return 有懸浮窗顯示在桌面上返回true,沒(méi)有的話返回false。 
 */ 
 public static boolean isWindowShowing() { 
 return smallWindow != null || bigWindow != null; 
 } 
 
 /** 
 * 如果WindowManager還未創(chuàng)建,則創(chuàng)建一個(gè)新的WindowManager返回。否則返回當(dāng)前已創(chuàng)建的WindowManager。 
 * 
 * @param context 
 * 必須為應(yīng)用程序的Context. 
 * @return WindowManager的實(shí)例,用于控制在屏幕上添加或移除懸浮窗。 
 */ 
 private static WindowManager getWindowManager(Context context) { 
 if (mWindowManager == null) { 
 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
 } 
 return mWindowManager; 
 } 
 
 /** 
 * 如果ActivityManager還未創(chuàng)建,則創(chuàng)建一個(gè)新的ActivityManager返回。否則返回當(dāng)前已創(chuàng)建的ActivityManager。 
 * 
 * @param context 
 * 可傳入應(yīng)用程序上下文。 
 * @return ActivityManager的實(shí)例,用于獲取手機(jī)可用內(nèi)存。 
 */ 
 private static ActivityManager getActivityManager(Context context) { 
 if (mActivityManager == null) { 
 mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
 } 
 return mActivityManager; 
 } 
 
 /** 
 * 計(jì)算已使用內(nèi)存的百分比,并返回。 
 * 
 * @param context 
 * 可傳入應(yīng)用程序上下文。 
 * @return 已使用內(nèi)存的百分比,以字符串形式返回。 
 */ 
 public static String getUsedPercentValue(Context context) { 
 String dir = "/proc/meminfo"; 
 try { 
 FileReader fr = new FileReader(dir); 
 BufferedReader br = new BufferedReader(fr, 2048); 
 String memoryLine = br.readLine(); 
 String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:")); 
 br.close(); 
 long totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")); 
 long availableSize = getAvailableMemory(context) / 1024; 
 int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100); 
 return percent + "%"; 
 } catch (IOException e) { 
 e.printStackTrace(); 
 } 
 return "懸浮窗"; 
 } 
 
 /** 
 * 獲取當(dāng)前可用內(nèi)存,返回?cái)?shù)據(jù)以字節(jié)為單位。 
 * 
 * @param context 
 * 可傳入應(yīng)用程序上下文。 
 * @return 當(dāng)前可用內(nèi)存。 
 */ 
 private static long getAvailableMemory(Context context) { 
 ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); 
 getActivityManager(context).getMemoryInfo(mi); 
 return mi.availMem; 
 } 
 
} 

這個(gè)類負(fù)責(zé)了控制大懸浮窗,小懸浮窗的創(chuàng)建和移除,系統(tǒng)內(nèi)存使用百分比的計(jì)算等操作。

到這里基本所有的代碼都已經(jīng)寫完了,然后我們來(lái)看一下AndroidManifest.xml文件吧,

里面代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.demo.floatwindowdemo" 
 android:versionCode="1" 
 android:versionName="1.0" > 
 
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
 
 <uses-sdk 
 android:minSdkVersion="8" 
 android:targetSdkVersion="8" /> 
 
 <application 
 android:allowBackup="true" 
 android:icon="@drawable/ic_launcher" 
 android:label="@string/app_name" 
 android:theme="@style/AppTheme" > 
 <activity 
 android:name="com.demo.floatwindowdemo.MainActivity" 
 android:label="@string/app_name" > 
 <intent-filter> 
 <action android:name="android.intent.action.MAIN" /> 
 
 <category android:name="android.intent.category.LAUNCHER" /> 
 </intent-filter> 
 </activity> 
 
 <service android:name=".FloatWindowService"></service> 
 </application> 
 
</manifest> 

比較簡(jiǎn)單,記得把Activity和Service在里面注冊(cè)好,還有一個(gè)權(quán)限聲明需要添加的android.permission.SYSTEM_ALERT_WINDOW,表示需要用戶授權(quán)允許創(chuàng)建系統(tǒng)提示窗口,也就是我們的桌面懸浮窗。

好了,現(xiàn)在讓我們運(yùn)行一下項(xiàng)目吧,效果如下圖,主界面只有一個(gè)簡(jiǎn)單的按鈕,點(diǎn)擊按鈕后,Activity被關(guān)閉,小懸浮窗顯示在桌面上。其中顯示著當(dāng)前內(nèi)存使用的百分比。

            

小懸浮窗是可以自由拖動(dòng)的,如果打開了其它的應(yīng)用程序,小懸浮窗會(huì)自動(dòng)隱藏,回到桌面后小懸浮窗又會(huì)顯示出來(lái)。

           

如果點(diǎn)擊了小懸浮窗會(huì)彈出大懸浮窗來(lái),這里我們大懸浮窗做的比較簡(jiǎn)單,就只有兩個(gè)按鈕。大懸浮窗展示的時(shí)候手機(jī)的所有其它程序是不可點(diǎn)的,因?yàn)榻裹c(diǎn)都在懸浮窗上了。點(diǎn)擊返回按鈕會(huì)重新展示小懸浮窗,點(diǎn)擊關(guān)閉懸浮窗按鈕,Service也會(huì)一起停掉。



360手機(jī)衛(wèi)士的一鍵加速功能我們就不做了,就像獨(dú)孤九劍一樣,重要的是劍意而不是劍招,我相信大家學(xué)會(huì)了創(chuàng)建懸浮窗的基本原理后可以做出比360更有創(chuàng)意的東西。

如果大家還有什么疑問(wèn),請(qǐng)?jiān)谙旅媪粞浴?br />

對(duì)桌面懸浮窗感興趣的朋友可以繼續(xù)閱讀 Android桌面懸浮窗進(jìn)階QQ手機(jī)管家小火箭效果實(shí)現(xiàn) 。

源碼下載,請(qǐng)點(diǎn)擊這里

補(bǔ)充:有朋友跟我反應(yīng),上面的代碼在Android 3.0以上的系統(tǒng)運(yùn)行會(huì)崩潰,我看了一下,確實(shí)如此,主要是3.0之后想要獲取正在運(yùn)行的任務(wù),需要加上權(quán)限聲明。在AndroidManifest.xml中加入

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

即可解決此問(wèn)題。

相關(guān)文章

最新評(píng)論