Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2108740
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: Java

2009-03-05 13:40:25

Source code locates at:


Theory:
1, Monitor network
1.1, After received broadcast about WIFI and CONNECTIVITY state changed, then read /proc/self/net/dev to read the number of data transferred.

Useful class:
WifiManager:
    return instance by calling context.getSystemService(WIFI_SERVICE) ConnectivityManager:
    return instance by calling context.getSystemService(CONNECTIVITY_SERVICE)

2, Monitor Network State
a) Implement the connectivity broadcast receiver
public class NetworkStateChangesReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null )
    {
      Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
    if( mobNetInfo != null )
    {
      Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
  }
}

b) Declare the receiver and permission in AndroidManifest.xml
          android:exported="true">



c) Register the receiver
mReceiver = new NetworkStateChangesReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(mReceiver, filter);

Reference:
a)
b)

3, Monitor CPU

Useful class:
TelephonyManager:
    return instance by calling context.getSystemService(TELEPHONY_SERVICE);

阅读(1895) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~