gpt4 book ai didi

java - 所有 Android 版本的 Android 互联网连接

转载 作者:行者123 更新时间:2023-12-02 04:48:07 26 4
gpt4 key购买 nike

我正在寻找一种方法来检查和监听我的互联网连接,但由于最新的 Android 版本不支持在 Manifest 中注册广播,我正在寻找一种方法来实现适用于所有 Android 版本的东西。到目前为止我找不到任何解决方案。

我尝试将广播 Intent 过滤器添加到我的 list 文件中,但在较新的 Android 版本中不允许这样做。

<receiver
android:name="utilities.NetworkStateChangeReceiver"
android:exported="true"
android:enabled="true"
android:label="NetworkConnection" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
<小时/>
public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";

@Override
public void onReceive(Context context, Intent intent) {
Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isConnectedToInternet(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
}

private boolean isConnectedToInternet(Context context) {
final ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

boolean isMobile = false, isWifi = false;

NetworkInfo[] infoAvailableNetworks = cm.getAllNetworkInfo();

if (infoAvailableNetworks != null) {
for (NetworkInfo network : infoAvailableNetworks) {

if (network.getType() == ConnectivityManager.TYPE_WIFI) {
if (network.isConnected() && network.isAvailable())
isWifi = true;
}
if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
if (network.isConnected() && network.isAvailable())
isMobile = true;
}
}
}

return isMobile || isWifi;
}
}
<小时/>

我的接收器无法接收广播

最佳答案

我在 IntentFilter 中使用了几乎相同的逻辑,它对我来说效果很好,无需在 Manifest 文件中注册它。

界面:

public interface IReceiverStateChange {

void register(BroadcastReceiver broadcastReceiver);
void unregister(BroadcastReceiver broadcastReceiver);
}

实现:

public class NetworkReceiverStateChange implements IReceiverStateChange {

private Context mContext;
private Boolean mIsRegistered = false;

public NetworkReceiverStateChange(Context context) {
mContext = context;
}

@Override
public void register(BroadcastReceiver broadcastReceiver) {
if (!mIsRegistered) {
IntentFilter netWorkIntentFilter = new
IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
mContext.registerReceiver(broadcastReceiver, netWorkIntentFilter);
mIsRegistered = true;
}
}

@Override
public void unregister(BroadcastReceiver broadcastReceiver) {
if (mIsRegistered) {
mContext.unregisterReceiver(broadcastReceiver);
mIsRegistered = false;
}
}
}

注册所需的 Activity :

if (mNetworkStateChange == null) {
mNetworkStateChange = new NetworkReceiverStateChange(this);
}
mNetworkStateChange.register(mNetworkBroadcastReceiver);

在onDestroy中取消注册:

if (mNetworkStateChange != null) {
mNetworkStateChange.unregister(mNetworkBroadcastReceiver);
}

广播接收器对象:

    //The CONNECTIVITY_ACTION broadcast seems to be sticky on some devices (even 
though the documentation implies that it is not).
// This means that when you register the receiver it will immediately call
onReceive() with the most recently sent broadcast.
private final BroadcastReceiver mNetworkBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null && action.matches("android.net.conn.CONNECTIVITY_CHANGE")) {
<DO SOMETHING>
}
}
};

希望有帮助:)

关于java - 所有 Android 版本的 Android 互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465822/

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