This question already has answers here:
How do I compare strings in Java?
(23个答案)
4年前关闭。
您好,我想寻求有关如何从
主要活动
包com.example.asd;
活动xml
短信接收器
因为将使用
另外,如果您是从意图中获得的,则必须确保此String不为null。喜欢:
(23个答案)
4年前关闭。
您好,我想寻求有关如何从
onreceive
的broadcastreceiver
内部找到if语句的帮助。我试图根据接收到的值更改按钮背景。就像当我收到值1
时,按钮图像将更改为onn
。因为当我放置if else语句时它不起作用,但是如果我将btn.setBackgroundResource(R.drawable.onn);
放置在if else语句之前,则它可以正常工作,但是它违背了让应用显示哪个按钮处于on
状态的目的。我正在尝试创建一个将根据短信更新6个开关的应用程序。就像我收到111000一样,前三个开关会将图像更改为开。 TIA主要活动
包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();
}
}
活动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()
方法而不是==
比较字符串另外,如果您是从意图中获得的,则必须确保此String不为null。喜欢:
bin[0]=intent.getStringExtra("zero");
if(bin[0]!=null){
//Your if/else statements
}