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 8529367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:01:21+00:00 2026-06-11T09:01:21+00:00

I’m creating an sms app that handles receiving sms operation for 2 user defined

  • 0

I’m creating an sms app that handles receiving sms operation for 2 user defined lists. For each list I created an activity, and xml file such as favourite_list.xml and contact_list.xml. Inside these xml files there are 2 toggle buttons. What I’m trying to do is, whenever an sms receives, learn the status (on/off) of every toggle button.

Here is my SmsReceiver class

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ToggleButton;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];    
            String str="";
            for (int i=0; i<msgs.length; i++){                                                  
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();  
            }    
        }     
    }        
}

I know I should use findViewById() to reach the status of the buttons, but I can’t directly use it because SmsReceiver isn’t extended from Activity. So far, what I tried to use findViewById

1- Cast context to Activity

try{                    
    tb = (ToggleButton) ((Activity)context).findViewById(R.id.toggleButton3);
    if(tb.isChecked())
        Log.d("sms","SOUND-ON");
    else if(!tb.isChecked())
        Log.d("sms","SOUND-OFF");
}catch(Exception e){
    Log.d("sms","Error " + e);
}

It gives java.lang.ClassCastException: android.app.ReceiverRestrictedContext

2- Use LayoutInflater

try{
    LayoutInflater mInf = LayoutInflater.from(context);
    View myView = mInf.inflate(R.layout.activity_favorite_list, null);
    tb = (ToggleButton) myView.findViewById(R.id.toggleButton3);
    if(tb.isChecked())
        Log.d("sms","SOUND-ON");
    else if(!tb.isChecked())
        Log.d("sms","SOUND-OFF");
}catch(Exception e){
    Log.d("sms","Error" + e);
} 

This is not working too, because it gives only the status of Toggle Button when app is started. Please help, thanks.

  • 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-11T09:01:22+00:00Added an answer on June 11, 2026 at 9:01 am

    If you just have a ToggleButton in each of the two activities you could use two preferences fields that will be written in the activities each time you modify their status. In the BroadcastReceiver you would use the same preferences fields to see the ToggleButton‘s statuses.

    If you have a ToggleButton in each row of the lists from the two activities you would use the same approach only that will have to use another structure that holds all the values from all rows(like a String where each row’s ToggleButtonstatus will be a value separated by a delimiter(like |)).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this

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.