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 ?