App Automation Residential

1

I'm creating a home automation app, in it I have the main screen that by selecting the desired category it opens a second screen with the commands (example lights category, it opens the commands Light1, Light2, etc.), when clicking the ImageButton of the light1 it changes the icon (lightOff to lightOn), when returning to the main screen and enter again in the light category I lose my last state of the ImageButton. Example I opened the light category and left the Light1 on (ImageButton will change the icon to lightOn), more when I go out to the main screen and go back to the lights screen it opens with the same lightOff icon I letting the light access. How do I make the icon always keep its last state left, every time I open the screen? (Sorry if I was not clear and because it's kind of hard to explain what I want).

public class Home extends Activity{ 

        private ImageButton btnLuz1;
        private ImageButton btnLuz2;
        Button btnVoltar;
        private Set<String> botoesOn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.home);

            btnVoltar= (Button) findViewById(R.id.btnVoltar);
            btnVoltar.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent voltaTela = new Intent(Home.this, MainActivity.class);
                    Home.this.startActivity(voltaTela);
                    Home.this.finish();

                }
            });

            btnLuz1 = (ImageButton) findViewById(R.id.btnLuz1);
            btnLuz2 = (ImageButton) findViewById(R.id.btnLuz2);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            botoesOn = prefs.getStringSet("botoes", null);

            if(botoesOn == null){
                botoesOn = new HashSet<String>();
            }
            else{
                if(botoesOn.contains("botao1")){
                    btnLuz1.setImageResource(R.drawable.luzon);
                }
                if(botoesOn.contains("botao2")){
                    btnLuz1.setImageResource(R.drawable.luzon);
                }
            }
        }

        public void btnLuz1Click(View v){
            if(botoesOn.contains("botao1")){
                botoesOn.remove("botao1");
                btnLuz1.setImageResource(R.drawable.luzoff);
            }
            else{
                botoesOn.add("botao1");
                btnLuz1.setImageResource(R.drawable.luzon);
            }
        }

        public void btnLuz2Click(View v){
            if(botoesOn.contains("botao2")){
                botoesOn.remove("botao2");
                btnLuz2.setImageResource(R.drawable.luzoff);
            }
            else{
                botoesOn.add("botao2");
                btnLuz2.setImageResource(R.drawable.luzon);
            }
        }

        protected void onPause(){
            super.onPause();
            SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(this);
            prefs.edit().putStringSet("botoes", botoesOn).apply();
        }

    }

and xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Home" >

<ImageButton
    android:id="@+id/btnLuz1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="100dp"
    android:layout_marginTop="53dp"
    android:layout_toLeftOf="@+id/btnLuzes"
    android:background="#00000000"
    android:onClick="bt1Click"
    android:src="@drawable/luzoff" />

<ImageButton
    android:id="@+id/btnLuz2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/btnLuz1"
    android:layout_marginLeft="150dp"
    android:background="#00000000"
    android:onClick="bt2Click"
    android:src="@drawable/luzoff" />

<Button
    android:id="@+id/btnVoltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/btnLuzes"
    android:layout_marginBottom="98dp"
    android:layout_marginRight="95dp"
    android:text="Voltar" />

Error Log:

05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.view.View$1.onClick(View.java:4007)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.view.View.performClick(View.java:4780)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.view.View$PerformClick.run(View.java:19866)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.os.Handler.handleCallback(Handler.java:739)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.os.Looper.loop(Looper.java:135)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.app.ActivityThread.main(ActivityThread.java:5257)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at java.lang.reflect.Method.invoke(Native Method)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at java.lang.reflect.Method.invoke(Method.java:372)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-07 11:50:45.466: E/AndroidRuntime(2422): Caused by: java.lang.NoSuchMethodException: bt1Click [class android.view.View]
05-07 11:50:45.466: E/AndroidRuntime(2422):     at java.lang.Class.getMethod(Class.java:664)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at java.lang.Class.getMethod(Class.java:643)
05-07 11:50:45.466: E/AndroidRuntime(2422):     at android.view.View$1.onClick(View.java:4000)
05-07 11:50:45.466: E/AndroidRuntime(2422):     ... 10 more
    
asked by anonymous 06.05.2015 / 14:45

2 answers

1

You can use SharedPreference to save button states.

Begin by declaring a HashSet that will hold the buttons that are ON:

private Set<String> botoesOn;

In each of the methods onClick() of the buttons

if(botoesOn.contais("botao1"){
    botoesOn.remove("botao1");
    //Passar o botão para OFF
}
else{
    botoesOn.add("botao1");
    //Passar o botão para ON
}

Replace in each of the methods "botao1" with "botao2" , "botao3" , etc.

In the onPause() method, save the HashSet to SharedPreference :

protected void onPause(){
    super.onPause();
    SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(this);
    prefs.edit().putStringSet("botoes", botoesON).apply();
}

In the onCreate() method, get HashSet and update the buttons according to their previous state:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
botoesOn = prefs.getStringSet("botoes", null);

if(botoesOn == null){
    botoesOn = new HashSet<String>();
}
else{
    if(botoesOn.contains("botao1")){
        // colocar botao1 ON
    }
    if(botoesOn.contains("botao2")){
        // colocar botao2 ON
    }
    if(botoesOn.contains("botao3")){
        // colocar botao3 ON
    }
    ... //Repetir para todos os botões
    ...
}  

All together is like this:

public class TesteActivity extends Activity {

    private ImageButton btnLuz1;
    private ImageButton btnLuz2;
    private Set<String> botoesOn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.teste);

        btnLuz1 = (ImageButton) findViewById(R.id.btnLuz1);
        btnLuz2 = (ImageButton) findViewById(R.id.btnLuz2);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        botoesOn = prefs.getStringSet("botoes", null);

        if(botoesOn == null){
            botoesOn = new HashSet<String>();
        }
        else{
            if(botoesOn.contains("botao1")){
                btnLuz1.setImageResource(R.drawable.luzon);
            }
            if(botoesOn.contains("botao2")){
                btnLuz1.setImageResource(R.drawable.luzon);
            }
        }
    }

    public void btnLuz1Click(View v){
        if(botoesOn.contains("botao1")){
            botoesOn.remove("botao1");
            btnLuz1.setImageResource(R.drawable.luzoff);
        }
        else{
            botoesOn.add("botao1");
            btnLuz1.setImageResource(R.drawable.luzon);
        }
    }

    public void btnLuz2Click(View v){
        if(botoesOn.contains("botao2")){
            botoesOn.remove("botao2");
            btnLuz2.setImageResource(R.drawable.luzoff);
        }
        else{
            botoesOn.add("botao2");
            btnLuz2.setImageResource(R.drawable.luzon);
        }
    }

    protected void onPause(){
        super.onPause();
        SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putStringSet("botoes", botoesOn).apply();
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F3F3F3">

    <ImageButton
        android:id="@+id/btnLuz1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/luzoff" 
        android:onClick="btnLuz1Click"/>

    <ImageButton
        android:id="@+id/btnLuz2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/luzoff" 
        android:onClick="btnLuz2Click"/>

</LinearLayout>
    
06.05.2015 / 16:27
1

There are different ways to do this:

  • Save button state in the database (What I recommend)

  • Receive modifications made to your command activity using the onActivityResult ( ) , and then, when returning to the activity of commands, you pass the last state stored. Using Intent

  • >
06.05.2015 / 15:23