设为首页 加入收藏

TOP

Android下仿一个优化大师的流量悬浮控件(一)
2014-11-24 07:43:29 来源: 作者: 【 】 浏览:6
Tags:Android 一个 优化 大师 流量 悬浮 控件

在平台项目里面做过一个service弹出对话框的功能。这个也差不多。


下面说下这个demo的功能点吧:


1.使用service来弹出此悬浮框,从而保证能长期存在。


2.使用window manager来控制悬浮框漂浮在所有view的上层。参数具体设置见代码。


3.使用TrafficStats来检测网络流量状态。


4.使用ConnectivityManager 来对Wifi,3G等网络状态进行检测。


5.使用handler在线程中异步刷新主界面。


废话不多说了,上代码,一看就明了:


package com.wenix;


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 =

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android与PHP服务器交互实例源码 下一篇Android自定义控件的属性

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)