如何在 Android 中使用 BroadcastReceiver onReceive 更新 UI?

How to update the UI with BroadcastReceiver onReceive in Android?

我想在 Java 中根据我的 Android 应用程序中的 WiFi 状态更新 UI。我无法更新 UI 以显示新字符串。请帮助我。

这是我的代码

public class MainActivity extends AppCompatActivity {
    IntentFilter intentFilter = new IntentFilter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(new NetworkConnectionReceiver(), intentFilter);

        TextView displayStatus = findViewById(R.id.displayStateTextView);
    }
}

class NetworkConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        Log.i("MyReceiver", Boolean.toString(wifiManager.isWifiEnabled()));
    }
}

您可以使用 interface

interface WifiStateListener{
  void onStateChanged(Boolean enabled);
}

然后添加一个构造函数将WifiStateListener传递给BroadcastReceiver

class NetworkConnectionReceiver extends BroadcastReceiver {
    private WifiStateListener mWifiStateListener;
    
    public NetworkConnectionReceiver(WifiStateListener wifistateListener){
        mWifiStateListener = wifistateListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if(mWifiStateListener != null){
            mWifiStateListener.onStateChanged(wifiManager.isWifiEnabled());
        }
        Log.i("MyReceiver", Boolean.toString(wifiManager.isWifiEnabled()));
    }
}

而在 Activity 中,注册时 receiver 您可以通过 interface

registerReceiver(new NetworkConnectionReceiver(enabled -> {
    //do based on wifi state change
}), intentFilter);

其他选项是使用 EventBus、Observables 等

使用此代码:

public class MainActivity extends AppCompatActivity {
...

    BroadcastReceiver br = new BroadcastReceiver(){
        @Override
        public void onReceive( Context context, Intent intent ){
           //update UI here directly
           View view = findViewById( R.id.example );
        }
    };

    @Override
    protected void onResume(){
        super.onResume();
        // Check state here
        ...
        IntentFilter filter = new IntentFilter();
        filter.addAction( ConnectivityManager.CONNECTIVITY_ACTION );
        registerReceiver( br, filter);
    }

    @Override
    protected void onPause(){
        super.onPause();
        unregisterReceiver( br );
    }

...
}

您可以使用 LocalBroadCastManager。

先在activity中写入broadCastReceiver 在 onResume 中使用 LocalBroadCastManager 注册该广播 在 onPause 中也取消注册 LocalBroadCastManager 的广播 而不是编写互联网连接广播并调用您在 activity

中注册的 LocalBroadCastManager
    private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           if(intent.getAction() == "local_broadcast_update_UI"){
              updateUI()
           }
        }
    };

  @Override
  protected void onResume(){
     super.onResume();
     LocalBroadcastManager.getInstance(this).registerReceiver( broadCastReceiver, new 
     IntentFilter("local_broadcast_update_UI"));   
  }

  @Override
  protected void onPause(){
     super.onPause();
  LocalBroadcastManager.getInstance(this).unregisterReceiver("local_broadcast_update_UI");
  }

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
           LocalBroadcastManager.getInstance(context).sendBroadcast(new 
           Intent("local_broadcast_update_UI"));
        }
    }
}