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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:23:03+00:00 2026-05-30T21:23:03+00:00

I am trying to broadcast a toast message with the following code extending Activity

  • 0

I am trying to broadcast a toast message with the following code extending Activity. But the broadcast is not received by another Activity, the toast is not displayed. Can someone solve my error? The main activity is SendBroadcast.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SendBroadcast extends Activity {

    public static String BROADCAST_ACTION =
                             "com.unitedcoders.android.broadcasttest.SHOWTOAST";

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

    public void sendBroadcast(View v) {
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(broadcast);
    }
}

Toast Display Activity is ToastDisplay.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

public class ToastDisplay extends Activity {

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "received",
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SendBroadcast.BROADCAST_ACTION);
        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }
}

and manifest.xml is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.broad"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SendBroadcast"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".ToastReceiver" >
            <intent-filter>
                <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
  • 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-05-30T21:23:04+00:00Added an answer on May 30, 2026 at 9:23 pm

    There can be two types of broacast: static and dynamic. Static are those that are declared in the manifest file. Dynamic can be declared during runtime. You combined these two types of broadcast in one broadcast.

    To declare a simple dynamic broadcast you can use the following code (that is based on your code). It will simply display toast message when activity is shown.

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Toast;
    
    public class BroadcastTestActivity extends Activity {
    
        public static String BROADCAST_ACTION =     
                                "com.unitedcoders.android.broadcasttest.SHOWTOAST";
        BroadcastReceiver br = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.w("Check", "Inside On Receiver");
                Toast.makeText(getApplicationContext(), "received",
                        Toast.LENGTH_SHORT).show();
            }
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            IntentFilter filter = new IntentFilter();
            filter.addAction(BROADCAST_ACTION);
            filter.addCategory(Intent.CATEGORY_DEFAULT);
            registerReceiver(br, filter);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            sendBroadcast();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(br);
        }
    
        public void sendBroadcast() {
            Intent broadcast = new Intent();
            broadcast.setAction(BROADCAST_ACTION);
            broadcast.addCategory(Intent.CATEGORY_DEFAULT);
            sendBroadcast(broadcast);
        }
    }
    

    So now instead of showing toast you can call your new activity. The following actions depend on your needs (what you want to do).

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

Sidebar

Related Questions

I'm just trying to create a simple broadcast in Androids activity but its not
I am trying to launch the Main Activity from a broadcast receiver. Can anyone
I have a broadcast receiver and I am trying to show a toast message
I am trying to implement time zone change in broadcast receiver but its not
On Android, I am trying to send a custom broadcast message using a custom
Hai am trying to get the gps location using BroadCast Receiver.its working fine,But i
I'm trying to find out whether broadcast messages will be sent to message only
I'm using Node.js 0.6.9, and am trying to send a datagram broadcast package. Code:
Following are the classes that i'm trying to implement, but i dont know where
I'm trying to send a broadcast and then let the server reply to it:

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.