簡體   English   中英

廣播接收器:為什么它不起作用?

[英]Broadcast Receiver: Why it doesn't work?

我有這個廣播接收器,它可以獲取並保持有關電池的最新信息。 有關電池的信息運作良好,從某種意義上來說,無需重新創建活動即可更新電池,而WiFi則不需要。 例如,如果我禁用WiFi,則不會更改“偏好摘要”(並且應該更改)。 為什么? 我該如何解決? 這是代碼。

@Override
protected void onStop()
{
    unregisterReceiver(batteryInfoReceiver);
    super.onStop();
}

@Override
protected void onRestart()
{
    this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    super.onRestart();
}

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

    int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
    String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
    int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;
    int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

    int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    if (sdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        boolean wCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;

        if(wCharge) {
            ricarica.setSummary("Charging (WiFi)");
        }
    }

    if(usbCharge) {
        ricarica.setSummary("Charging (USB)");
    }
    else if (acCharge){
        ricarica.setSummary("Charging (AC)");
    }

    else {
        ricarica.setSummary("Not charging");
    }

    livelloBatteria.setSummary("Battery at "+level+"%");
    livelloVoltaggio.setSummary(voltage+" mV");
    livelloTemperatura.setSummary(Integer.toString(temperature)+"° Celsius");
    tecnologia.setSummary(technology);

    int ip = wifiInfo.getIpAddress();
    int speed = wifiInfo.getLinkSpeed();
    String speedString = Integer.toString(speed);
    String mac = wifiInfo.getMacAddress();
    String ssid = wifiInfo.getSSID();
    String ipAddress = Formatter.formatIpAddress(ip);

    if (wifiMgr.isWifiEnabled()){

    if(mac!=null) {
        indirizzoMac.setSummary(mac);
    }
    else {
        indirizzoMac.setSummary("Unkown");
    }

    if(ipAddress!=null) {
    indirizzoIp.setSummary(ipAddress);
    }
    else {
        indirizzoIp.setSummary("Unkown");
    }

    if(ssid!=null) {
        indirizzoSsid.setSummary(ssid);
    }
    else {
        indirizzoSsid.setSummary("Unkown");
    }

    if(speedString!=null) {
        velocità.setSummary(speedString+" Mbps");
    }

    else {
        velocità.setSummary("Unkown");
    }

    }

    else {
        indirizzoIp.setSummary("WiFi disabled");
        indirizzoSsid.setSummary("WiFi disabled");
        velocità.setSummary("WIFi disabled");
        indirizzoMac.setSummary(mac);
    }
    }
    };

我認為您的接收器尚未注冊以進行wifi連接更改,請嘗試向其添加另一個過濾器:

IntentFilter filter= new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
this.registerReceiver(this.batteryInfoReceiver,filter);

您還可以添加連接更改操作以進行過濾,以便在網絡連接更改時調用onReceive:

filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM