android使用的数据,Android上的数据使用情况

您可以为WiFi和移动网络注册BroadcastReceiver,然后将时间保存在BroadcastReceiver类的onReceive()方法中

public class NetworkReceiver extends BroadcastReceiver {

private final String TAG = "WifiReceiver";

public static final String WIFI_DISCONNECTED_TIME = "wifi_disconnected_time";

public static final String WIFI_CONNECTED_TIME = "wifi_connected_time";

@Override

public void onReceive(Context context, Intent intent) {

MyLog.e(TAG, "Broadcast");

if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

String networkStateText;

switch (netInfo.getState()) {

case DISCONNECTING:

networkStateText = "DISCONNECTING";

break;

case DISCONNECTED:

networkStateText = "DISCONNECTED";

//Save the time internet disconnected here

context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).edit().putLong(WIFI_DISCONNECTED_TIME, System.currentTimeMillis()).commit();

break;

case CONNECTING:

networkStateText = "CONNECTING";

break;

case CONNECTED:

networkStateText = "CONNECTED";

//Save the time internet connected here

context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).edit().putLong(WIFI_CONNECTED_TIME, System.currentTimeMillis()).commit();

break;

case SUSPENDED:

networkStateText = "SUSPENDED";

break;

case UNKNOWN:

networkStateText = "UNKNOWN";

break;

default:

networkStateText = "No State";

break;

}

MyLog.e(TAG, "Broadcast > NetworkState: " + networkStateText);

}

}

}

不要忘记放入权限android.permission.ACCESS_NETWORK_STATE并将接收器添加到android清单。

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

然后你可以在Activity课程中从SharedPreferences获得时间。

祝好运。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值