gpt4 book ai didi

java - 网络监听器 Android

转载 作者:IT老高 更新时间:2023-10-28 13:21:21 25 4
gpt4 key购买 nike

我想检查 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>

关于java - 网络监听器 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1783117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com