App Automaticao Residencial

Asked

Viewed 164 times

1

I’m creating a home automation app, in it I have the main screen that when selecting the desired category it opens a second screen with the commands (example category lights, it opens the commands Luz1, Luz2, etc.), by clicking on the Imagebutton of the light1 it changes the icon (luzOff to Luzon), when I return to the main screen and enter again in the category of lights I lose my last state of Imagebutton. Example I opened the light category and left the Light1 on (Imagebutton will change the icon to Luzon), more when I go out to the main screen and go back to the screen of lights it opens with the light iconOff even if I leave the light on. How do I make the icon always keep its last state left, every time I open the screen ? (I’m sorry if I wasn’t clear and why 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
  • It is one more mistake due to the translation of the name of my test methods into the names you use in the question. In the XML in the attributes android:onClick substitute bt1Click for btnLuz1Click and bt2Click for btnLuz2Click.

2 answers

1

There are different ways to do this:

  • Save button status in the bank (What I recommend).

  • Receive modifications made to your commands Activity using the method onActivityResult(), and then, when you return to command Activity, you pass the last stored state. Using Intent (dice extra)

  • Right, but I had once done without bank only I lost the code, at the moment I do not think to use a bank I think is unnecessary for my app. Do you know another way to do this by using a flag to check the Imagebutton every time I open the Activity Lights, or some other medium ? If not, much obg by the answer.

  • @Itallofreire If your system is home automation, you NEED a bank. How will the system behave when the power goes out, or the same is turned off? Will the lights stay on or will they return to the default setting of your program? Important data, mainly data that makes a difference in the real world, needs to be stored in a reliable database.

  • And because we have developed a circuit board (central) especially for this then when the power is cut and returns the central returns the last state that was left. I want to do without bank first to perform some tests. I will use the device (tablet or cell phone) to read the status of the plant, to check if it is lit, erased, tvOn, etc.. So I find unnecessary the bank tbm am layman in android programming and I’m having some difficulties. Even so obg by tip.

  • @Itallofreire, it is very recommended a database, You need to store various information about the user, such as preference, schedules, dimensions, etc. Storing all this in Shared Preferences is not very efficient...

  • @Renaro Santos - Right I understand, but I would have to learn everything about bank on android, because I’m layman on the subject, I would have to take course and I’m not available for this at the moment. Would you have any tips how to do that ? I’m running out of time tbm need to finish this project soon.

1


You can use Sharedpreference to store button states.

Start by declaring a Hashset that will save the buttons that are ON:

private Set<String> botoesOn;

In each of the methods onClick() button

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" for "botao2","botao3", etc..

In the method onPause() keep the Hashset on Sharedpreference:

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

In the method onCreate() get the 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
    ...
}  

Together it’s 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.