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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:10:00+00:00 2026-06-11T03:10:00+00:00

I want to trigger a Broadcast receiver from the notification. When I click on

  • 0

I want to trigger a Broadcast receiver from the notification. When I click on a button which is on the notification it shows this error:

"Unable to instantiate receiver com.example.testservice.myBroad:
java.lang.ClassCastException: com.example.testservice.myBroad cannot
be cast to android.content.BroadcastReceiver"

**updated/ edited and its working now **

  1. Can you please help to handle 2 buttons from notification to broadcast receiver?
    How can I pass extra value from notification broadcast trigger to a receiver that whether its play button pressed or pause?

  2. Now my button working but when I click on notification text it does not take me into my activity. Any help?

I write this code for 2 buttons with extra on intent.

 RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
            layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
            Intent clickIntent = new Intent();
            clickIntent.putExtra("button","pause");
            clickIntent.setAction(ACTION_DIALOG);
           
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
            layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
            builder.setContent(layout);
            
            
             layout = new RemoteViews(getPackageName(), R.layout.notification);
            layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
            Intent click = new Intent();
            clickIntent.putExtra("Button","play");
            clickIntent.setAction(ACTION_DIALOG);
           
            PendingIntent pi1 = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, click, pendingFlag);
            layout.setOnClickPendingIntent(R.id.notification_button1,pi1);
            builder.setContent(layout);

myBroad receiver file

Bundle extrasBundle = intent.getExtras();
    String str = (String) extrasBundle.get("button");       
    Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

    context.stopService(new Intent(context, myPlayService.class));

Here is my code:

void showNotification() {
     int pendingRequestCode = 0;
        int pendingFlag = 0;

        final Resources res = getResources();
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        Intent intent = new Intent(MainActivity.this,myBroad.class);
        PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_action_search)
                .setAutoCancel(true)
                .setTicker("this is notification")
                .setContentIntent(getDialogPendingIntent("Tapped the notification entry."));

        
            // Sets a custom content view for the notification, including an image button.
            RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
            layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
            Intent clickIntent = new Intent();
            clickIntent.setAction(ACTION_DIALOG);
           
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
            layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
            builder.setContent(layout);

            // Notifications in Android 3.0 now have a standard mechanism for displaying large
            // bitmaps such as contact avatars. Here, we load an example image and resize it to the
            // appropriate size for large bitmaps in notifications.
            Bitmap largeIconTemp = BitmapFactory.decodeResource(res,
                    R.drawable.notification_default_largeicon);
            Bitmap largeIcon = Bitmap.createScaledBitmap(
                    largeIconTemp,
                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
                    false);
            largeIconTemp.recycle();

            builder.setLargeIcon(largeIcon);

        notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
 }
    

    PendingIntent getDialogPendingIntent(String dialogText) {
        return PendingIntent.getActivity(
                this,
                dialogText.hashCode(), // Otherwise previous PendingIntents with the same
                                       // requestCode may be overwritten.
                new Intent(ACTION_DIALOG)
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                0);
    }
 

myBroad.class

public class myBroad extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "received", Toast.LENGTH_SHORT).show();
    
        context.stopService(new Intent(context, myPlayService.class));  
    }
}

Manifest file is:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
           

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
          </activity>
          
     <receiver android:name=".myBroad">
        <intent-filter >
<action android:name="android.intent.action.MEDIA_BUTTON"/>
    </intent-filter>

        </receiver>
  
    <service android:name="com.example.testservice.myPlayService" android:icon="@drawable/ic_action_search" android:label="@string/app_name" android:enabled="true"/>
    
    
</application>
  • 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-11T03:10:02+00:00Added an answer on June 11, 2026 at 3:10 am

    Because myBroad is an activity object, and not a broadcast object…

    Instead of extending (inheritance) from activity, and then making an internal class which extends broadcastreceiver, then just inherit directly from broadcastreceiver.

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

Sidebar

Related Questions

I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit
If I want to trigger an error in my interpreter I call this function:
I have a broadcast receiver which will launch an Activity onReceive. When launched, this
I want to trigger events from a Firefox extension, specifically click events. I've tried
I want to trigger a specific input button in a form from another forms
I want to trigger this function, which uses the jquery's post method, when a
I want to trigger accordion when I click minimize button or to be more
i want to trigger an applet button on click of a normal html button
I want to trigger a long running background process (coded as a service) from
I want to trigger an event with a button that changes the visibility of

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.