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);
阅读(1888) | 评论(0) | 转发(0) |