【问题标题】:Wifi state change in broadcast receiver send change to MainActivit广播接收器中的 Wifi 状态更改将更改发送到 MainActivit
【发布时间】:2017-03-17 06:03:12
【问题描述】:

我正在构建一个应用程序来监控 wifi 变化。这是一个基于 MainActivity 和 WiFiReceiver 类的非常简单的应用程序。

MainActivity如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent

这只是提供一个 UI。

此外,我还有扩展广播接收器的类 WifiReceiver。

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            Log.d("WifiReceiver", "Have Wifi Connection");
            sendEnteringHomeRequest();
        }
        else {
            Log.d("WifiReceiver", "Don't have Wifi Connection");
            sendLeavingHomeRequest();
        }

    }

这是一个监控 wifi 的基本广播接收器。

我想使用 sendLeavingHomeRequest 和 sendEnteringHomeRequest 向 MainActivity 发送消息,以便显示某些内容或执行其他操作。我对活动和广播接收器之间的通信很感兴趣

有什么想法吗?

【问题讨论】:

    标签: java android android-activity broadcastreceiver


    【解决方案1】:

    您应该使用 BroadcastReceiver 作为 MainActivity 中的内部类。分别在 onResume() 和 onPause() 中注册和取消注册您的接收器。 这里是Sample code

    【讨论】:

      【解决方案2】:

      我对活动和广播接收器之间的通信很感兴趣

      您可以为此目的使用 LocalBroadcastManager

      在接收端:

      • 首先注册本地广播接收器
      • 然后在 onReceive 中处理传入的 Intent 数据。

          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver, 
          new IntentFilter("Your_IntentFilter_string"));
          }
        
          public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
               if (intent != null ) {
               String str= intent.getStringExtra("key");
               //Get all your data from intent and do what you want 
               }
             }
           }
        };
        

      发送结束时:

         Intent intent = new Intent("Your_IntentFilter_string");
         intent.putExtra("key", "My Data");
         //Put your all data using put extra 
      
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
      

      【讨论】:

      • 我需要在 Manifest 中做些什么吗?
      猜你喜欢
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多