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

  • SEARCH
  • Home
  • 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 9228683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:23:07+00:00 2026-06-18T05:23:07+00:00

I need some help with an android project I am working on. I am

  • 0

I need some help with an android project I am working on. I am trying to use switch preferences to send a certain text. Basically, it the user switches the switch from off to on, I want the phone to send a text saying “on”. Then when the user turns the switch from on to off, it sends a text saying “off”. All I need is to be able to see what the current state of the switch is and then if it’s off, call a “turn on” method and vice-versa.

I’ve never asked a question like this, so I don’t really know what part of my code to post.(If asked, I can post most of my code.) I think it has something to do with the onPreferenceChangeListener, but I’m not sure how to implement it. Any ideas?

Edit: Here is the main activity class:

public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

public static final String KEY_ROOM1_SWITCH = "switch_room_1";

private SwitchPreference mSwitchPreference1;

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//This is a sample of one of 4 switches that are being used. They are all basically the same, but with different variables
    if (key.equals(KEY_ROOM1_SWITCH)) {
        boolean checkedornot1;
        SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
        checkedornot1 = myPreference.getBoolean("switch_room_1", false);
        if (checkedornot1 = true)
        mSwitchPreference1.setChecked(true);
        else
        mSwitchPreference1.setChecked(false);
    }
  }
}

Do I need to grab the value that is stored in the shared preferences and make my choice based on that? or is there something else I am missing?

  • 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-18T05:23:08+00:00Added an answer on June 18, 2026 at 5:23 am
    1. Edit your class that extends PreferenceActivity and add the private variable: private OnSharedPreferenceChangeListener listener;

    2. Create and register your listener within the onResume method:

        public void onResume() {
        super.onResume();
        listener = new OnSharedPreferenceChangeListener() {
          public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
            if (key.contains("your switchpreference key name")
            if (sp.getBoolean("your switchpreference key name",false) {
              sendOnSMS();
            } else {
              sendOffSMS();
            }
        }
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(listener);
      }
      
    3. Unregister your listener within the onPause method:

      public void onPause() {
        super.onPause();
        PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(listener);
      }
      
    4. Implement the sendOnSMS and sendOffSMS methods.

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

Sidebar

Related Questions

I am trying to create my first Android app and need some help. Basically
i need some help in addition to android-layouts. For eyample: Actually I use a
I need some help with properly versioning my Android project with Git/Eclipse. I have
I am working on a Android project. I need a small help in that.
I'm working with Android and Spinners and I need some help. I have a
i need some help on my android project. i have a canvas and i
I need some help dealing with three Threads in Android One thread is the
I need help with android project inside NetBeans. I Just opened project which is
I am currently working on an android project and I am trying to start
I need some help. In my android app I have a ViewFlipper that contains

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.