在android中收到消息时,图像应该被更改

时间:2012-10-16 13:50:09

标签: java android events

    package com.example.nhx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {
    ImageView imageView1;
    ImageView imageView2;
    Button ON, OFF;
    String msg = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---

        if (msg.contains("LED ON")) {
            Toast.makeText(context, "HELLO WORLD", Toast.LENGTH_SHORT).show();
        }

        if (msg.contains("LED OFF")) {
            Toast.makeText(context, "HELLO", Toast.LENGTH_SHORT).show();
        }

        if (msg == "LED ON") {

            imageView1.setVisibility(imageView1.VISIBLE);
            imageView2.setVisibility(imageView2.INVISIBLE);

        }

        if (msg == "LED OFF") {
            imageView2.setVisibility(imageView2.VISIBLE);
            imageView1.setVisibility(imageView1.INVISIBLE);
        }
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        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 += "SMS from " + msgs[i].getOriginatingAddress();
                // str += " :";
                str += msgs[i].getMessageBody().toString();
                str += msg;
                // str += "\n";

            }

        }

        if (msg.contains("LED ON")) {
            Toast.makeText(context, "HELLO WORLD", Toast.LENGTH_SHORT).show();
        }

        else {
            Toast.makeText(context, "HELLO", Toast.LENGTH_SHORT).show();

        }

        if (msg == "LED ON") {

            imageView1.setVisibility(imageView1.VISIBLE);
            imageView2.setVisibility(imageView2.INVISIBLE);

        }

        if (msg == "LED OFF") {
            imageView2.setVisibility(imageView2.VISIBLE);
            imageView1.setVisibility(imageView1.INVISIBLE);
        }

        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

    }

    private void startactivity(Intent i) {

    }
}

    package com.example.nhx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class blinkMe extends Activity {
    Button ON;
    Button OFF;
    ImageView imageView1;
    ImageView imageView2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ON = (Button) findViewById(R.id.ON);
        OFF = (Button) findViewById(R.id.OFF);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        imageView2 = (ImageView) findViewById(R.id.imageView2);

        ON.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {

                sendSMS("5556", "LED ON");
                OFF.setClickable(true);
                imageView1.setVisibility(imageView1.VISIBLE);
                imageView2.setVisibility(imageView2.INVISIBLE);

            }

            private void sendSMS(String phoneNumber, String message) {
                // TODO Auto-generated method stub
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, null, null);
                ON.setClickable(true);

            }
        });

        OFF.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                ON.setClickable(true);
                sendSMS("5554", "LED OFF");
                /* Intent i = new Intent(); */

                imageView2.setVisibility(imageView2.VISIBLE);
                imageView1.setVisibility(imageView1.INVISIBLE);

            }

            private void sendSMS(String phoneNumber, String message) {
                // TODO Auto-generated method stub
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, null, null);

            }
        });

    }

}

在我的应用程序中,我有两个按钮,如打开和关闭,两个图像相互重叠,红色和绿色。当我点击按钮绿色图像应该出现前言,当我点击关闭时,红色图像应该出现前言,当点击按钮时,应该将一条消息发送到预定义的数字意味着当点击按钮时发生两个事件 1.相应的图像切换前言 2.消息被发送到预定义的号码

现在问题出现在发送消息之后,预期接收者现在根据收到的消息向我们发送消息,图像应该自动改变,就像接收方回复的绿色图像应该是前言,如果他回复了,那么红色图像应该来前言

0 个答案:

没有答案