广播接收器中的Wifi状态更改将更改发送到MainActivit

时间:2017-03-17 06:03:13

标签: java android android-activity broadcastreceiver

我正在构建一个监控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

这只是提供一个用户界面。

此外,我还有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发送消息以显示内容或执行其他操作。我对活动和广播接收器之间的通信感兴趣

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  

我对活动与广播接收者之间的沟通感兴趣

您可以将LocalBroadcastManager用于此目的

在接收结束时:

  • 首先注册LocalBroadcast Receiver
  • 然后在onReceive中处理传入的意图数据。

      @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);