0

I am building an app to monitor wifi change. It's a very simple app based on MainActivity and WiFiReceiver class.

The MainActivity is as below :

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

this is just providing a UI.

In addition, I have the class WifiReceiver which extend a broadcast receiver.

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();
        }

    }

This is a basic broadcast receiver which monitor wifi.

I want to use sendLeavingHomeRequest and sendEnteringHomeRequest to send a message to the MainActivity in order to display something or do something else. I am insterested in the communication between the activity and the broadcast receiver

Any idea ?

Seb
  • 2,929
  • 4
  • 30
  • 73

2 Answers2

0

You should use BroadcastReceiver as an inner class inside MainActivity. Register and unregister your receiver in onResume() and onPause() respectively. Here is Sample code

Community
  • 1
  • 1
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

I am insterested in the communication between the activity and the broadcast receiver

You can use the LocalBroadcastManager for this purpose

On Receiving end:

  • First register LocalBroadcast Receiver
  • Then handle incoming intent data in 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 
           }
         }
       }
    };
    

On Sending End:

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

  LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Darish
  • 11,032
  • 5
  • 50
  • 70