2 u010212704 u010212704 于 2015.06.30 18:39 提问

android广播在activity中接收不到

我想在activity中接收广播的wifi状态信息,但是我写的代码接收不到,求教啊~!
1.发送的广播类

```public class ConnectionChangeReceiver extends BroadcastReceiver {
Context context;
public ConnectionChangeReceiver(Context context) {
// TODO Auto-generated constructor stub
//this.wifiStateImage=imageView;
this.context=context;
int strength=getStrength(context);
//wifiStateImage.setImageLevel(strength);

}
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    System.out.println(intent.getAction());
    System.out.println("程序进入!");
    if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION))
    {
        int strength=getStrength(context);
        //System.out.println("当前信号 "+strength);
        //wifiStateImage.setImageLevel(strength);
    }else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
        //System.out.println("网络状态改变");
        Intent intent4 = new Intent();
        intent.setAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intent.putExtra("NETWORK_STATE_CHANGED_ACTION", "网络状态改变");
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent4);
        //System.out.println("断开连接");
        NetworkInfo info=intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(info.getState().equals(NetworkInfo.State.DISCONNECTED))
        {//如果断开连接
            //wifiStateImage.setImageLevel(0);
            Intent intent3 = new Intent();
            intent.setAction(WifiManager.EXTRA_NETWORK_INFO);
            intent.putExtra("NETWORK_INFO", "断开连接");
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent3);
            //System.out.println("断开连接");
        }
    }else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
    {
        //WIFI开关
        int wifistate=intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,WifiManager.WIFI_STATE_DISABLED);
        if(wifistate==WifiManager.WIFI_STATE_DISABLED)
        {//如果关闭
            //wifiStateImage.setImageLevel(0);
            //System.out.println("wifi关闭");

            Intent intent2 = new Intent();
            intent.setAction(WifiManager.EXTRA_WIFI_STATE);
            intent.putExtra("WIFI_STATE", "wifi关闭");
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent2);
        }
    }

}
public int getStrength(Context context)
{
    WifiManager wifiManager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    if (info.getBSSID() != null) {
        int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
        //Wifi源名称
        String ssid = info.getSSID();
        System.out.println("wifi名字= "+ssid);
        Intent intent = new Intent();
        intent.setAction("cn.abel.action.broadcast");
        intent.putExtra("Ssid", ssid);

        return strength;

    }
    return 0;
}

}

2.activity中注册广播的方法


``` @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpage_main);
        mActionBar.hide();
        init();
        //listwifi();
        ConnectionChangeReceiver myBroadcastReciver = new ConnectionChangeReceiver(this);
        System.out.println("OnCreate!");
        //System.out.println("onCreateView");
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

        localBroadcastManager.registerReceiver(myBroadcastReciver, filter);
    }

为什么我程序都没有进入ConnectionChangeReceiver类中?求大神指教~!

3个回答

ljcmeng
ljcmeng   2015.06.30 20:09

你这是要监听wifi状态变化吗?

u010212704
u010212704 嗯 是的 用来监听WIFI状态的变化
2 年多之前 回复
bhdgx
bhdgx   2015.06.30 23:16

你应该是要监听WIFI的状态,有设置相应的权限了吗?

u010212704
u010212704 权限设置了
2 年多之前 回复
liyuanjinglyj
liyuanjinglyj   2015.07.01 23:33

本地广播在4.0以后就被抛弃了,肯定接受不到。

Csdn user default icon
上传中...
上传图片
插入图片