函数原型:
int select(int maxfd,fd_set *readset,fd_set *writeset,fd_set *exceptset,const struct
timeval *timeout);
参数一:当前进程的最大文件描述符个数(这里可以以这样理解)
参数二:读可行性集合(若在该集合中表示可读)
参数三:写可行性集合(若在该集合中表示可写)
参数四:异常集合(若产生异常)------该参数我没有深入理解;自己可以查阅相关手册
参数五:用于设置select为阻塞或是非阻塞
参数五解析:设置阻塞或是非阻塞:当此参数为NULL时表示为阻塞;当为0(timeout指向的内存
中的值)根本不等待立即返回;当为非0(同上一行)设定select检测的等待时间。
阻塞:NULL
当第五个参数设定为NULL时,那我们在检测的描述字,必须要有可读,或可写,或错误异常,才
会返回值
应用:
对于在server中的I/O多路复用,我们用select检测套接字等待某个事件发生,或是新客服连接或
是数据、PIN、RST到达
重点理解:
(这里可能和我们之前理解的accept()来记录连接套接字有区别,但实际上是一样的,我们要肯
定一点:实际上客户端请求server连接时和accept无关,accept()不过是来记录这个连接的套接
字,因而在accept前便可检测客服端是否发送来连接请求信息)
非阻塞:timeout设定
struct timeval time1 = {
.tv_sec = 0,
.tv_usec = 0,
};
struct timeval time2 = {
.tv_sec = 3,
.tv_usec = 0,
};
用到time1表示select根本不等待就立即返回0,处理下面的数据(比如打印等:)
用到time2 表示select会等待3秒,如果在限定的时间内没有检测到事件的发生(某个事件,或是
新客服端连接,或是数据到达),就返回0;
FD_ZERO(fdset); 将fdset的所有位清零
FD_SET(fd,fdset); 向fdset中添加fd(turn on the bit for fd in fdset t改变fdset
中的位)
FD_CLR(fd,fdset); 从fdset集合中删除fd,关闭fd在fdset中对应的位
FD_ISSET(fd,fdset); 判断fd是否是在fdset中(这里我要在下面详细说明一下这个用法)
fd_set readfd; 用于可读性检查的集合
应用:套接字的判定
FD_ISSET(s,readfd) ;判断套接字是否可读(这里在该集合中(表示连接请求,或者数据到达)
)) 为非0;否则为0;
这回在我的select阻塞版的server中体现出来,请仔细分析:
效果图:
需要注意的是,本代码只有部署在真机上时才会有效果,当不熟在模拟器时很可能不会有效果
代码实现:
1、traffic_manager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_appicon"
android:layout_width="35dip"
android:layout_height="35dip"
android:scaleType="fitXY"
android:src="/blog_article/@drawable/ic_launcher/index.html" />
<TextView
android:id="@+id/tv_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_appicon"
android:text="我最摇摆"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_apptarffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="20MB"
android:textColor="@android:color/white"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_appname"
android:layout_toRightOf="@id/iv_appicon"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_apptx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="80dp"
android:text="上传:20MB"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_apprx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载:20MB"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
2、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_dark"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2g/3g" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="wifi" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_traffic_manager_mobile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="200Mb" />
<TextView
android:id="@+id/tv_traffic_manager_wifi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="50MB" />
</TableRow>
</TableLayout>
<SlidingDrawer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:content="@+id/lv_traffic_manager_content"
android:handle="@+id/handle"
android:orientation="vertical" >
<ImageView
android:id="@id/handle"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="/blog_article/@drawable/notification/index.html" />
<ListView
android:id="@id/lv_traffic_manager_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</SlidingDrawer>
</LinearLayout>
3、TrafficManagerActivity
package com.njupt.testtracfficstate;
import java.util.ArrayList;
import java.util.List;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final int SUCCESS_GET_TRAFFICINFO = 0;
/** Called when the activity is first created. */
private TextView tv_traffic_manager_mobile;
private TextView tv_traffic_manager_wifi;
private ListView lv_traffic_manager_content;
private TrafficManagerService trafficManagerService;
private List<TrafficInfo> trafficInfos;
private List<TrafficInfo> realTrafficInfos;
private TrafficManagerAdapter mAdapter;
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS_GET_TRAFFICINFO:
mAdapter = new TrafficManagerAdapter(getApplicationContext());
lv_traffic_manager_content.setAdapter(mAdapter);
break;
default:
break;
}
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 2g3g 接收流量
TrafficStats.getMobileRxBytes();
//2g/3g 接收的包
TrafficStats.getMobileRxPackets();
//2g/3g 上传的流量
TrafficStats.getMobileTxBytes();
//2g/3g 上传的包
TrafficStats.getMobileTxPackets();
// 手机总共接收的流量
TrafficStats.getTotalRxBytes();
// 手机总共上传的流量
TrafficStats.getTotalTxBytes();
//得到么个应用程序接收的流量
TrafficStats.getUidRxBytes(uid);
TrafficStats.getUidTxBytes(uid);*/
tv_traffic_manager_mobile = (TextView) findViewById(R.id.tv_traffic_manager_mobile);
tv_traffic_manager_wifi = (TextView) findViewById(R.id.tv_traffic_manager_wifi);
lv_traffic_manager_content = (ListView) findViewById(R.id.lv_traffic_manager_content);
tv_traffic_manager_mobile.setText(TextFormat.formatByte(getMobileTotal()));
tv_traffic_manager_wifi.setText(TextFormat.formatByte(getWifiTotal()));
trafficManagerService = new TrafficManagerService(this);
new Thread(){
public void run() {
trafficInfos = trafficManagerService.getLauncherTrafficInfos();
realTrafficInfos = new ArrayList<TrafficInfo>();
for(TrafficInfo info:trafficInfos){
if(TrafficStats.getUidRxBytes(info.getUid()) == -1 && TrafficStats.getUidTxBytes(info.getUid()) == -1){
//
}else{
realTrafficInfos.add(info);
}
}
Message msg = new Message();
msg.what = SUCCESS_GET_TRAFFICINFO;
mHandler.sendMessage(msg);
};
}.start();
}
/**
* 2g/3g的总流量
* @return
*/
private long getMobileTotal(){
long mobile_rx = TrafficStats.getMobileRxBytes();
long mobile_tx = TrafficStats.getMobileTxBytes();
return mobile_rx + mobile_tx;
}
/**
* 得到手机的总流量
* @return
*/
private long getTotal(){
return TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();
}
/**
* 得到wifi的总流量
* @return
*/
private long getWifiTotal(){
return getTotal() - getMobileTotal();
}
static class ViewHolder{
ImageView iv_appicon;
TextView tv_appname;
TextView tv_apptraffic;
TextView tv_apptx;
TextView tv_apprx;
}
private final class TrafficManagerAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public TrafficManagerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return realTrafficInfos.size();
}
public Object getItem(int position) {
return realTrafficInfos.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder holder = null;
if(convertView != null){
view = convertView;
holder = (ViewHolder) view.getTag();
}else{
view = mInflater.inflate(R.layout.traffic_manager_item, null);
holder = new ViewHolder();
holder.iv_appicon = (ImageView) view.findViewById(R.id.iv_appicon);
holder.tv_appname = (TextView) view.findViewById(R.id.tv_appname);
holder.tv_apptraffic = (TextView) view.findViewById(R.id.tv_apptarffic);
holder.tv_apptx = (TextView) view.findViewById(R.id.tv_apptx);
holder.tv_apprx = (TextView) view.findViewById(R.id.tv_apprx);
view.setTag(holder);
}
TrafficInfo trafficInfo = realTrafficInfos.get(position);
holder.iv_appicon.setImageDrawable(trafficInfo.getAppicon());
holder.tv_appname.setText(trafficInfo.getAppname());
int uid = trafficInfo.getUid();
long tx = TrafficStats.getUidTxBytes(uid);
if(tx == -1){
tx = 0;
}
long rx = TrafficStats.getUidRxBytes(uid);
if(rx == -1){
rx = 0;
}
long total = tx + rx;
holder.tv_apptraffic.setText(TextFormat.formatByte(total));
holder.tv_apptx.setText("上传:" + TextFormat.formatByte(tx));
holder.tv_apprx.setText("下载:" + TextFormat.formatByte(rx));
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
4、TrafficManagerService
package com.njupt.testtracfficstate;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
public class TrafficManagerService {
private Context context;
private PackageManager pm;
public TrafficManagerService(Context context) {
this.context = context;
pm = context.getPackageManager();
}
/**
* 得到所有能启动的应用
* @return
*/
public List<TrafficInfo> getLauncherTrafficInfos(){
List<TrafficInfo> trafficInfos = new ArrayList<TrafficInfo>();
//查询能够启动的应用程序
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//ResolveInfo 就类似于一个IntentFilter
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for(ResolveInfo info:resolveInfos){
ApplicationInfo appInfo = info.activityInfo.applicationInfo;
Drawable appicon = appInfo.loadIcon(pm);
String appname = appInfo.loadLabel(pm).toString();
String packageName = appInfo.packageName;
int uid = appInfo.uid;
trafficInfos.add(new TrafficInfo(appicon, appname, packageName, uid));
}
return trafficInfos;
}
}
5、TrafficInfo
package com.njupt.testtracfficstate;
import android.graphics.drawable.Drawable;
public class TrafficInfo {
private Drawable appicon;
private String appname;
private String packageName;
private int uid;
public TrafficInfo() {
super();
}
public TrafficInfo(Drawable appicon, String appname, String packageName,
int uid) {
super();
this.appicon = appicon;
this.appname = appname;
this.packageName = packageName;
this.uid = uid;
}
public Drawable getAppicon() {
return appicon;
}
public void setAppicon(Drawable appicon) {
this.appicon = appicon;
}
public String getAppname() {
return appname;
}
public void setAppname(String appname) {
this.appname = appname;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Override
public String toString() {
return "TrafficInfo [appicon=" + appicon + ", appname=" + appname
+ ", packageName=" + packageName + ", uid=" + uid + "]";
}
}
6、TextFormat
package com.njupt.testtracfficstate;
import java.text.DecimalFormat;
public class TextFormat {
// 23.87MB 00.87MB/KB byte
public static String formatByte(long data) {
DecimalFormat format = new DecimalFormat("##.##");
if (data < 1024) {
return data + "bytes";
} else if (data < 1024 * 1024) {
return format.format(data / 1024f) + "KB";
} else if (data < 1024 * 1024 * 1024) {
return format.format(data / 1024f / 1024f) + "MB";
} else if (data < 1024 * 1024 * 1024 * 1024) {
return format.format(data / 1024f / 1024f / 1024f) + "GB";
} else {
return "超出统计返回";
}
}
}
首先声明转载URL:http://chenfeng0104.iteye.com/blog/1186850
有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息,而直接调用View组件的getWidth()、getHeight()、getMeasuredWidth()、getMeasuredHeight()、getTop()、getLeft()等方法是无法获取到真实值的,只会得到0。这是因为View组件布局要在onResume回调后完成。下面提供实现方法,onGlobalLayout回调会在布局完成时自动调用
final LinearLayout imageViewContainer = (LinearLayout)findViewById(R.id.crop_photo_image_view_container);
imageViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
boolean isFirst = true;//默认调用两次,这里只让它执行一次回调
@Override
public void onGlobalLayout() {
if (isFirst) {
isFirst = false;
//现在布局全部完成,可以获取到任何View组件的宽度、高度、左边、右边等信息
Log.i("CDH", "Global W:"+imageViewContainer.getMeasuredWidth()+" H:"+imageViewContainer.getMeasuredHeight());
}
}
});