在平台项目里面做过一个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);