public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到管理者
ConnectivityManager manager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//得到链接信息对象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//进行网络判断
if (networkInfo!=null&&networkInfo.isConnected()){
Toast.makeText(MainActivity.this,"网络连上了",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"网络断开了",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到管理者
ConnectivityManager manager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//得到链接信息对象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//进行网络判断
if (networkInfo!=null&&networkInfo.isConnected()){
Toast.makeText(MainActivity.this,"网络连上了",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"网络断开了",Toast.LENGTH_LONG).show();
}
}
}
//添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>