标签:ems iss public over 关闭 available new roman dac
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册广播
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);
}
/**
* 活动被关闭后执行
*/
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver {//定义一个内部类用来接收广播
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context, "网络状态发生改变!",
// Toast.LENGTH_SHORT).show();
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
Toast.makeText(context, "已连接网络!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "当前网络不可用!", Toast.LENGTH_SHORT).show();
}
}
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
意思是允许程序访问有关GSM网络信息。
标签:ems iss public over 关闭 available new roman dac
原文地址:https://www.cnblogs.com/qikeyishu/p/9216990.html