我想从通知中触发广播接收器。当我点击通知上的按钮时,它会显示以下错误:
“无法实例化接收器com.example.testservice.myBroad: java.lang.ClassCastException:com.example.testservice.myBroad不能 被强制转换为android.content.BroadcastReceiver“
**更新/编辑及其现在工作** 1)嘿伙计们,请帮助处理从通知到广播接收器的2个按钮 如何将通知广播触发器中的额外值传递给接收器,无论其播放按钮是按下还是暂停? 2)现在我的按钮工作,但当我点击通知文本时,它不会让我进入我的活动。任何帮助
我为2个具有额外意图的按钮编写此代码
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接收器文件
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));
这是我的代码:
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));
}
}
清单文件是:
<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>
答案 0 :(得分:3)
因为myBroad是一个活动对象,而不是广播对象......
不是从活动扩展(继承),而是创建一个扩展broadcastreceiver的内部类,而只是直接从broadcastreceiver继承。
答案 1 :(得分:1)
在您的方法getDialogPendingIntent
中,您应该使用PendingIntent.getBroadcast
代替PendingIntent.getActivity
。这会导致PendingIntent
推送广播。
并解决这个问题:
public class myBroad extends BroadcastReceiver
答案 2 :(得分:0)
异常明确表示 - 您的myBroad
必须像现在一样延伸BroadcastReceiver
,而不是Activity
。
PS:您应该考虑切换到正确的命名协议。所以它应该是MyBroad
(类)但是myBroad
(该类的对象)。即:
MyBroad myBroad = new MyBroad();