Hello I would like to ask for help on how to get the if else statements found below inside a onreceive
from a broadcastreceiver
. Im trying to change the button background depending on the value received. like when I receive the value 1
the button image will change to onn
. Because it does not work when I place if else statements, but if I place the btn.setBackgroundResource(R.drawable.onn);
before the if else statements it works fine however it defeats the purpose of having the app show which button is at on
state. I'm trying to create an app that will update 6 switches depending on the text message. Like if I receive 111000 the first three switches will change image to on. TIA
Main activity
package com.example.asd;
import java.lang.reflect.Array;
import java.text.BreakIterator;
import android.R.array;
import android.R.layout;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.widget.TextView;
public class MainActivity extends Activity {
String[] bin={"0","0","0","0","0","0"};
String[] bin1={"0","0","0","0","0","0"};
Button zero,one,two,three,four,five,update,refresh;
IntentFilter intentFilter;
TextView tv1;
private final BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//---display the SMS received in the TextView---
init();
zero=(Button)findViewById(R.id.one);
bin[0]=intent.getStringExtra("zero");
if (bin[0]!=null && bin[0].equalsIgnoreCase("1")) {
zero.setBackgroundResource(R.drawable.onn);
}
else {
zero.setBackgroundResource(R.drawable.off);
}
one=(Button)findViewById(R.id.two);
bin[1]=intent.getStringExtra("one");
if (bin[1]!=null && bin[1].equalsIgnoreCase("1")) {
one.setBackgroundResource(R.drawable.onn);
}
else {
one.setBackgroundResource(R.drawable.off);
}
two=(Button)findViewById(R.id.three);
bin[2]=intent.getStringExtra("two");
if (bin[2]!=null && bin[2].equalsIgnoreCase("1")) {
two.setBackgroundResource(R.drawable.onn);
}
else {
two.setBackgroundResource(R.drawable.off);
}
three=(Button)findViewById(R.id.four);
bin[3]=intent.getStringExtra("three");
if (bin[3]!=null && bin[3].equalsIgnoreCase("1")) {
three.setBackgroundResource(R.drawable.onn);
}
else {
three.setBackgroundResource(R.drawable.off);
}
four=(Button)findViewById(R.id.five);
bin[4]=intent.getStringExtra("four");
if (bin[4]!=null && bin[4].equalsIgnoreCase("1")) {
four.setBackgroundResource(R.drawable.onn);
}
else {
four.setBackgroundResource(R.drawable.off);
}
five=(Button)findViewById(R.id.six);
bin[5]=intent.getStringExtra("five");
if (bin[5]!=null && bin[5].equalsIgnoreCase("1")) {
five.setBackgroundResource(R.drawable.onn);
}
else {
five.setBackgroundResource(R.drawable.off);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---intent to filter for SMS messages received---
intentFilter = new IntentFilter();
zero=(Button)findViewById(R.id.one);
one=(Button)findViewById(R.id.two);
two=(Button)findViewById(R.id.three);
three=(Button)findViewById(R.id.four);
four=(Button)findViewById(R.id.five);
five=(Button)findViewById(R.id.six);
refresh=(Button)findViewById(R.id.refresh);
update=(Button)findViewById(R.id.update);
intentFilter.addAction("SMS_RECEIVED_ACTION");
zero.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[0]=="1") {
bin[0]="0";
updateBtn();
}
else {
bin[0]="1";
updateBtn();
}
}
});
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[1].equals("1")) {
bin[1]="0";
updateBtn();
}
else {
bin[1]="1";
updateBtn();
}
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[2].equals("1")) {
bin[2]="0";
updateBtn();
}
else {
bin[2]="1";
updateBtn();
}
}
});
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[3].equals("1")) {
bin[3]="0";
updateBtn();
}
else {
bin[3]="1";
updateBtn();
}
}
});
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[4].equals("1")) {
bin[4]="0";
updateBtn();
}
else {
bin[4]="1";
updateBtn();
}
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bin[5].equals("1")) {
bin[5]="0";
updateBtn();
}
else {
bin[5]="1";
updateBtn();
}
}
});
refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateBtn();
String s="";
for (int i = 0; i < 6; i++) {
s+= bin[i];
}
String upToNCharacters = s.substring(0, Math.min(s.length(), 6));
Toast.makeText(getBaseContext(),upToNCharacters+"00", Toast.LENGTH_LONG).show();
s="";
}
});
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
updateBtn();
//Toast.makeText(getBaseContext(),s0+s1+s2+s3+s4+s5+"00", Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(),bin[0]+bin[1]+bin[2]+bin[3]+bin[4]+bin[5]+"00", Toast.LENGTH_LONG).show();
}
});
}
private void updateBtn() {
for (int i = 0; i < 6; i++) {
if (bin[i]==null) {
bin[i]="0";
}
else
{
bin[i]=bin[i];
}
}
if (bin[0]=="1") {
zero.setBackgroundResource(R.drawable.onn);
}
else if(bin[0]=="0"){
zero.setBackgroundResource(R.drawable.off);
}
if (bin[1]=="1") {
one.setBackgroundResource(R.drawable.onn);
}
else if(bin[1]=="0"){
one.setBackgroundResource(R.drawable.off);
}
if (bin[2]=="1") {
two.setBackgroundResource(R.drawable.onn);
}
else if(bin[2]=="0"){
two.setBackgroundResource(R.drawable.off);
}
if (bin[3]=="1") {
three.setBackgroundResource(R.drawable.onn);
}
else if(bin[3]=="0"){
three.setBackgroundResource(R.drawable.off);
}
if (bin[4]=="1") {
four.setBackgroundResource(R.drawable.onn);
}
else if(bin[4]=="0"){
four.setBackgroundResource(R.drawable.off);
}
if (bin[5]=="1") {
five.setBackgroundResource(R.drawable.onn);
}
else if(bin[5]=="0"){
five.setBackgroundResource(R.drawable.off);
}
}
private void init() {
// TODO Auto-generated method stub
for (int i = 0; i < 6; i++) {
bin[i]="0";
}
}
@Override
protected void onResume() {
//---register the receiver---
registerReceiver(intentReceiver, intentFilter);
super.onResume();
}
@Override
protected void onPause() {
//---unregister the receiver---
unregisterReceiver(intentReceiver);
super.onPause();
}
}
activity 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:background="@drawable/back"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/one"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/two"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/three"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/five"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/four"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/six"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/five"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/off"
android:rotation="90" />
<Button
android:id="@+id/refresh"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="19dp"
android:layout_marginLeft="31dp"
android:background="@drawable/button" />
<Button
android:id="@+id/update"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="69dp"
android:layout_marginLeft="31dp"
android:background="@drawable/button" />
</RelativeLayout>
Smsreceiver
package com.example.asd;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String[] bin=new String[6];
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs[i].getMessageBody().toString();
bin[i]=msgs[i].getMessageBody().toString();
}
//---send a broadcast intent to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("zero", bin[0]);
broadcastIntent.putExtra("one", bin[1]);
broadcastIntent.putExtra("two", bin[2]);
broadcastIntent.putExtra("three", bin[3]);
broadcastIntent.putExtra("four", bin[4]);
broadcastIntent.putExtra("five", bin[5]);
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
}