赞
踩
//动态注册监听网络变化 //1.创建一个类继承自BroadcastReceiver,即创建广播接收器,当有广播到来时, //onReceive()方法就会得到执行,在这个方法中处理收到广播后的逻辑 class ChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if(networkInfo !=null && networkInfo.isAvailable()) Toast.makeText(context,"network is available",Toast.LENGTH_SHORT).show(); else{ Toast.makeText(context,"network is unavailable",Toast.LENGTH_SHORT).show(); } } } private IntentFilter intentFilter; private ChangeReceiver changeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。