网络监听器Android
我想检查Android手机的网络何时关闭。我可以捕获该事件吗?
我没有得到正确的API或任何可以解释相同内容的示例。如果有人做过或有任何示例链接将非常有帮助。
回答:
新的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:
<!-- Needed to check when the network connection changes --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
你的AndroidManifest.xml中“ application”元素下的新xml:
<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 的全部内容, 来源链接: utcz.com/qa/410054.html