网络监听器的Andr​​oid [英] Network listener Android

查看:133
本文介绍了网络监听器的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,当手机在Android的网络熄灭。我可以捕捉事件? 我没有得到适当的API或任何例子可以解释相同。如果有人做了或者例如链接将是非常有益的。 先谢谢了。

I want to check when the network of phone in Android goes off. Can I capture that event? I am not getting the proper API or any example which would explain the same. If anyone had done or any example links would be really helpful. Thanks in advance.

推荐答案

新的Java类:

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();
    }
  }
}

在你的Andr​​oidManifest.xml中新的XML下的清单的元素:

New xml in your AndroidManifest.xml under the "manifest" element:

<!-- Needed to check when the network connection changes -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

在你的Andr​​oidManifest.xml中新的XML下的应用程序的元素:

New xml in your AndroidManifest.xml under the "application" element:

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

这篇关于网络监听器的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆