Prakhar Sharma
Prakhar Sharma

Reputation: 21

Android app for Wifi automation

I want to develop an android app that triggers Wifi..

When we open the app, if our Wifi is on, it will toast Connected Message
Else, a button having a text connect will be displayed and when you click that button, button text will change to connected and your Wifi is turned on.

I have done this .. but my sir asked me to introduce such change that once we press button it changes from connect to connected and Wifi is on..

Now, if we manually turn off the Wifi in our setting and then we open our paused app, then the button will show connect option again.

I want to introduce automation in my app. My sir gave me hint that there is some helper class in android which keep on calling the method or some event handler that handles the event that occurs outside the app, but I still have no idea how to do that.

Please help me, thanks!

Here is the java code of my app:

public class MainActivity extends ActionBarActivity implements OnClickListener {
    WifiManager wf;
    static Button buttn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttn = (Button) findViewById(R.id.button);
        buttn.setOnClickListener(this);
        wf = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }

    public void onClick(View v) {
        if (v == buttn) {
            wf.setWifiEnabled(true);
            buttn.setText("connected");
            Toast.makeText(this, "Wifi Connected", Toast.LENGTH_LONG).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 activity in AndroidManifest.xml.
        int id = item.getItemId();
        // noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Views: 156

Answers (1)

Greg Hilston
Greg Hilston

Reputation: 2424

I'd take a look at an Activity's Life cycle here

I believe you'll be interested in the "onResume" and able to detect if WiFi is still connected there.

Upvotes: 0

Related Questions