适用于Wifi自动化的Android应用

时间:2015-06-07 11:50:29

标签: java android

我想开发一个触发Wifi的Android应用程序..

当我们打开应用程序时,如果我们的Wifi打开,它将会连接已连接的消息另外,将显示一个带有文本连接的按钮, 当您单击该按钮时,按钮文本将更改为已连接,并且您的Wifi已打开。

我做过这个......但是 我的先生让我介绍这样的改变,一旦我们按下按钮,它就会从连接变为连接,Wifi开启..

现在,如果我们在设置中手动关闭Wifi然后打开暂停的应用程序,则该按钮将再次显示连接选项。

我想在我的应用中介绍自动化。我的先生告诉我暗示在android中有一些辅助类继续调用方法处理应用程序外发生的事件的某个事件处理程序,但我仍然不知道怎么做。

请帮助我,谢谢!

以下是我的应用程序的java代码:

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

1 个答案:

答案 0 :(得分:0)

我要查看活动的生命周期here

我相信你会对" onResume"感兴趣。并且能够检测WiFi是否仍然连接在那里。