2
    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. 消息被发送到预定义的号码

现在问题出现在发送消息后,预期的接收者现在根据收到的消息向我们发送消息,图像应该自动更改,例如如果接收者回复打开,则绿色图像应该是前言,如果他回复关闭,那么红色图像应该是前言。

4

0 回答 0