在平台項目裡面做過一個service彈出對話框的功能。這個也差不多。
下面說下這個demo的功能點吧:
1.使用service來彈出此懸浮框,從而保證能長期存在。
2.使用window manager來控制懸浮框漂浮在所有view的上層。參數具體設置見代碼。
3.使用TrafficStats來檢測網絡流量狀態。
4.使用ConnectivityManager 來對Wifi,3G等網絡狀態進行檢測。
5.使用handler在線程中異步刷新主界面。
廢話不多說了,上代碼,一看就明了:
package com.wenix;
import Android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.wenix.util.NetworkUtil;
public class TopFloatService extends Service {
protected static final String TAG = "TopFloatService";
protected static final int WHAT = 0x123456;
WindowManager wm = null;
WindowManager.LayoutParams wmParams = null;
View view;
ImageView downloadIv;
TextView flowTxt;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private String flowInfoStr;
private Handler mHandler;
@Override
public void onCreate() {
super.onCreate();
// 獲取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService("window");
// 設置LayoutParams(全局變量)相關參數
wmParams = new WindowManager.LayoutParams();
// setForeground(true);
view = LayoutInflater.from(this).inflate(R.layout.floating, null);
downloadIv = (ImageView) view.findViewById(R.id.downloadingImgBtn);
flowTxt = (TextView) view.findViewById(R.id.downloadingInfoTxt);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (!Thread.interrupted()) {
if (NetworkUtil.isNetworkAvailable(getApplicationContext())) {
float reciveBytes = TrafficStats.getMobileRxBytes() / 1024.0f;
float sendBytes = TrafficStats.getMobileTxBytes() / 1024.0f;
String flowInfo = getApplicationContext().getResources().getString(R.string.networkflow);
flowInfoStr = String.format(flowInfo, reciveBytes, sendBytes);
Log.i(TAG, "reciveBytes=" + reciveBytes + ",sendBytes=" + sendBytes);
Message msg = new Message();
msg.what = WHAT;
mHandler.sendMessage(msg);
}
}
}
}).start();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (WHAT == msg.what) {
flowTxt.setText(flowInfoStr);
}
}
};
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
createView();
}
private void createView() {
wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
// 該類型提供與用戶交互,置於所有應用程序上方,但是在狀態欄後面
// TYPE_TOAST TYPE_SYSTEM_OVERLAY 在其他應用上層 在通知欄下層 位置不能動鳥
// TYPE_PHONE 在其他應用上層 在通知欄下層
// TYPE_PRIORITY_PHONE TYPE_SYSTEM_ALERT 在其他應用上層 在通知欄上層 沒試出來區別是啥
// TYPE_SYSTEM_ERROR 最頂層(通過對比360和天天動聽歌詞得出)
// 用別的TYPE還出報錯... 也希望大家補充一下
wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 不接受任何按鍵事件
wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角
// 以屏幕左上角為原點,設置x、y初始值
wmParams.x = 0;
wmParams.y = 0;
// 設置懸浮窗口長寬數據
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.format = PixelFormat.RGBA_8888;
wm.addView(view, wmParams);
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
// 25是系統狀態欄的高度,也可以通過方法得到准確的值,自己微調就是了
y = event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY() + view.getHeight() / 2;
return false;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
return false;
}
return true;
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
private void updateViewPosition() {
// 更新浮動窗口位置參數
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(view, wmParams);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}