final TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new PhoneStateListener(){
@Override
public void onDataConnectionStateChanged(int state) {
switch(state){
case TelephonyManager.DATA_DISCONNECTED://网络断开
System.out.println("网络断开1 ");
Toast.makeText(MainUiHomePage.this, "网络断开!",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.DATA_CONNECTING://网络正在连接
System.out.println("网络断开2 ");
Toast.makeText(MainUiHomePage.this, "网络正在连接...",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.DATA_CONNECTED://网络连接上
System.out.println("网络断开3 ");
Toast.makeText(MainUiHomePage.this, "网络已连接!",
Toast.LENGTH_LONG).show();
break;
}
}
}, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);