java - 如何在广播接收器上使用 if else 语句?

标签 java android broadcastreceiver

您好,我想寻求帮助,了解如何获取下面在 onreceive 中找到的 if else 语句。来自broadcastreceiver 。我试图根据收到的值更改按钮背景。就像我收到值 1 时一样按钮图像将更改为onn 。因为当我放置 if else 语句时它不起作用,但如果我放置 btn.setBackgroundResource(R.drawable.onn);在 if else 语句之前它工作正常,但是它违背了让应用程序显示哪个按钮位于 on 的目的。状态。我正在尝试创建一个应用程序,该应用程序将根据短信更新 6 个开关。就像如果我收到 111000 前三个开关会将图像更改为打开。 TIA 主要 Activity 包 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>

短信接收器

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

最佳答案

你必须使用

 if (bin[0].equals("1")) {
                zero.setBackgroundResource(R.drawable.onn);
            }
            else {
                zero.setBackgroundResource(R.drawable.off);
            }

因为字符串将与 equals() 方法进行比较,而不是与 ==

此外,如果您从 Intent 中获取该字符串,则必须确保该字符串不为空。喜欢:

 bin[0]=intent.getStringExtra("zero");
 if(bin[0]!=null){


   //Your if/else statements

 }

关于java - 如何在广播接收器上使用 if else 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766660/

相关文章:

android - ActionBar 操作项未显示

android - 无法在 Android 10 上从 BroadcastReceiver 启动 Activity

Java从对象类型获取函数

java - 在 Eclipse 和命令行中运行 javac ant 时的类文件大小差异

java - 多线程Java小程序弹跳球

java - 在 Java 中返回字符串方法?

android - 悄悄更新正在进行的通知

android - 如何刷新 FragmentPagerAdapter 的 fragment ?

android - 移动屏幕锁定时广播接收器不会触发

java - 有没有办法将 i + 1 包含在广播类函数的 for 循环中?