Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:53:16+00:00 2026-06-15T16:53:16+00:00

Oh man, I have googled my pants off and had no luck with errors

  • 0

Oh man, I have googled my pants off and had no luck with errors for a couple of days now.

In short, I want to receive an sms that has a message device : command so e.g. SPA:ON

My app is super simple, all I want to do is change a text that displays the status of a device in the MainActivity ( so i.e. change the @string/somevalue in the XML) so that when I receive a SPA:ON message my TextView changes from the initial string of OFF to the new command text of ON.

Whats the best way to do this given my code below??

If it’s not possible – what do I do?

Thanks

public class MainActivity extends Activity {

    private static Button buttonSend;
    private static EditText textPhoneNo;
    private static EditText textSMS;
    private static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
        textSMS = (EditText) findViewById(R.id.editTextSMS);

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
        public void onClick(View v) {   

            String phoneNo = textPhoneNo.getText().toString();
            String sms = textSMS.getText().toString();  

            if (phoneNo.length()>0 && sms.length()>0) 
            {
                sendSMS(phoneNo, sms);
            }
            else
            {
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
            }
        }
        });
    }  

    //---sends a SMS message to another device---
    public void sendSMS(String phoneNo, String message) {      

        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                      
                }
            }
        }, new IntentFilter(DELIVERED));         

        try 
        {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
        } 
    catch (Exception e) 
        {
            Toast.makeText(getApplicationContext(),
                "SMS faild, please try again.",
                Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }       
}

And the SMS code to receive (and attempt to do the text update) is:

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
    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 += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show(); 

        //---update the device status from the command---
        /* SOME HOW I WANT TO DO:

        String toast_msg = "None";
        String temp[];
        temp = str.split("\\:");
        String device = temp[0];
        String cmd = temp[1];

        TextView t = (TextView) findViewById(R.id.textViewSPAStatus);

        t.setText(cmd);

        Toast.makeText(getBaseContext(), 
                toast_msg, 
                Toast.LENGTH_SHORT).show(); 
        */
    }                       
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:53:17+00:00Added an answer on June 15, 2026 at 4:53 pm

    You cannot modify the xml, those values are permanent to the xml. What you can use instead:

    • SharedPreferences
    • SQLite Databases
    • A file

    SharedPreferences is the simplest to set up and works quite well, and although the text Preferences is part of the name, it is not limited to settings, since it’s a simple key-value pair storage option. So after setting it up, your app will use this as the centralized “command text” database. This means when your Activity is created/resumed you update the TextView with whatever the SharedPreferences has. Nice thing about SharedPreferences is that you can make a separate simple database just for your commands and unless the user deletes your app data, it is one of the safest storage options for values that your app needs persisting across reboots, exits, etc.

    Also, to ensure that your receiver can receive all the messages, you might want to consider making a background Service that registers your Recevier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

man git-gc doesn't have an obvious answer in it, and I haven't had any
My question is simple: if I have some class Man and I want to
I have Man class which also has 2 variable : description and name. At
I have a cloud and a man sprite, these are both drawn separately but
I have created an array Man: public main blah blah{ man = man[10]; }
i have the following script css: img { background-image:url(man.png); background-repeat:no-repeat; padding-top:6px; padding-bottom:60px; padding-left:10px; padding-right:10px;
I have a doubt regarding the backlog value in listen system call. From man
Here is the man page for git show-ref -d . They also have an
I googled and searched in the boost's man, but didn't find any examples. May
I downloaded the iPhone SDK from the iPhone dev center a couple of days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.