how to Trigger broadcast receiver from notification
I want to trigger Broadcast receiver from notification. When I click on a button which is on 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)Hey guys can you please help to handle 2 buttons from notification to broadcast reciver How can i pass extra value from notification broadcast trigger to receiver that wether 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 button 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>
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.
In your method getDialogPendingIntent
you should use PendingIntent.getBroadcast
instead of PendingIntent.getActivity
. This will cause your PendingIntent
to push a broadcast .
And fix this:
public class myBroad extends BroadcastReceiver
Exception says it clearly - your myBroad
have to extend BroadcastReceiver
, not Activity
as it does now.
PS: you should consider switching to proper naming convenction. So it should be MyBroad
(class) but myBroad
(object of that class). ie:
MyBroad myBroad = new MyBroad();
链接地址: http://www.djcxy.com/p/63530.html
上一篇: Twitter API:如何获取用户ID,谁喜欢特定的推文?
下一篇: 如何从通知中触发广播接收器