通过广播监听网络状态

主要代码块(Activity):

public class ConnectionChangeReceiver extends
BroadcastReceiver   
{   
@Override  
public void onReceive( Context context, Intent intent )   
{   
ConnectivityManager connectivityManager = 
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE );   
NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();   
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE );   
if ( activeNetInfo != null )   
{   
Toast.makeText( context, "Active Network Type : " + 
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
}   
if( mobNetInfo != null )   
{   
Toast.makeText( context, "Mobile Network Type : " + 
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
}   
}   
}

android manifest.xml

  
< uses-permission android:name="android.permission.
ACCESS_NETWORK_STATE"/>   
< receiver android:name="com.receiver.ConnectionChangeReceiver"   
android:label="NetworkConnection">   
< intent-filter>   
< action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   
< /intent-filter>   
< /receiver>


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OuNuo5280/article/details/49890677
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

通过广播监听网络状态

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭