Android Open Source - arduino4home Main Activity






From Project

Back to project page arduino4home.

License

The source code is released under:

GNU General Public License

If you think the Android project arduino4home listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package recode.smartr3;
//from  www  .j  a v a  2  s. co  m
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    Button buttonVentilador;
    boolean isVentiladorOn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicy policy = new StrictMode.
                ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonVentilador = (Button) findViewById(R.id.button_led);
        buttonVentilador.setOnClickListener(this);

        if(isVentiladorOn == true){
            buttonVentilador.setText(R.string.button_off);

        }else{
            buttonVentilador.setText(R.string.button_on);
        }
    }


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

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.button_led:

                HttpClient client = new DefaultHttpClient();

                if (isVentiladorOn == true) {

                    try {
                        client.execute(new HttpGet("http://192.168.0.180/?lightoff"));
                        isVentiladorOn = false;
                        buttonVentilador.setText(R.string.button_off);

                    } catch (Exception e) {

                    }

                } else {

                    try {
                        client.execute(new HttpGet("http://192.168.0.180/?lighton"));
                        isVentiladorOn = true;
                        buttonVentilador.setText(R.string.button_on);

                    } catch (Exception e) {

                    }
                }
                break;

            default:
                break;

        }
    }
}




Java Source Code List

recode.smartr3.ApplicationTest.java
recode.smartr3.MainActivity.java