网络侦听器 Android [英] Network listener Android
本文介绍了网络侦听器 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查看 Android 手机的网络何时关闭.我可以捕获那个事件吗?
I want to check when the network of phone in Android goes off. Can I capture that event?
我没有得到正确的 API 或任何可以解释相同情况的示例.如果有人做过或任何示例链接会非常有帮助.
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.
推荐答案
新的 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();
}
}
}
AndroidManifest.xml 中manifest"元素下的新 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"/>
AndroidManifest.xml 中application"元素下的新 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>
这篇关于网络侦听器 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文