本文介绍了Android的 - 网络国家广播接收器没有收到意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要听网络变化的广播接收机 -

I have a broadcast receiver that needs to listen to network changes -

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity =
            intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        if(!noConnectivity)
        {
            //some stuff
        }
    }
};

我注册它使用 -

I register it using -

public void startListening() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(networkStateReceiver, filter);
}

我添加了以下权限在清单 -

I have added the following permission in the manifest -

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在networkStateReceiver仍然没有收到任何意图,当我打开手机,以飞行模式,切换WiFi关闭等我失去了一些东西?

The networkStateReceiver still does not receive any intent when I switch the phone to airplane mode, switch wifi off etc. Am I missing something?

推荐答案

您可以能够使用这样的:

You can able to use this:

目前@覆盖之前,先添加此

At first add this before @Override

private boolean isConnected = true;
private Context c;

然后使用它

//check for connectivity:
ConnectivityManager connectivityManager = (ConnectivityManager) 
c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo TestConnetion = connectivityManager.getActiveNetworkInfo();
if (TestConnetion == null){
isConnected=false;  
}

这篇关于Android的 - 网络国家广播接收器没有收到意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 07:02