当网络状态发生变化时,Android是否会播放意图,即从GSM到Wifi?如果是这样,我需要什么权限以及所谓的意图动作是什么?
我正在做一个通过应用程序使用Internet连接的应用程序.如果在使用应用程序时丢失了Internet连接,则应用程序将强行关闭.为避免这种情况,如果互联网不可用,我想显示一条警报消息.我怎样才能做到这一点.在登录时,我正在使用以下代码检查连接.但是我如何在后台为整个应用程序执行此操作.
private boolean haveInternet(){
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info==null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to disable internet while roaming, just return false
return true;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
谢谢..