I want to start the Broadcast receiver from a notification. When I click on the button that is in the notification, it shows this error:
"Cannot instantiate recipient com.example.testservice.myBroad: java.lang.ClassCastException: com.example.testservice.myBroad cannot be cast to android.content.BroadcastReceiver"
** updated / edited, and now it works ** 1) Hello guys, could you help with the processing of 2 buttons from the notification to the broadcast receiver. How can I pass an extra value from a notification trigger to a receiver that presses the play button or pauses? 2) now my button works, but when I click on the notification text, it does not lead me to my activity. any help
I am writing this code for 2 buttons with additional intention
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);
recipient file myBroad
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."));
myBroad.class
public class myBroad extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) {
Manifest file:
<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>