android - 如何从通知中触发广播接收器?

标签 android android-layout android-intent

我想从通知中触发广播接收器。当我点击通知上的按钮时,它会显示此错误:

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

**已更新/编辑并且现在可以使用**

  1. 你能帮忙处理从通知到广播接收器的 2 个按钮吗? 我如何将额外的值从通知广播触发器传递给接收器,无论它的播放按钮是按下还是暂停?

  2. 现在我的按钮可以工作了,但是当我点击通知文本时,它并没有让我进入我的 Activity 。有帮助吗?

我为 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));  
    }
}

list 文件是:

 <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>

最佳答案

因为 myBroad 是一个 Activity 对象,而不是广播对象...

与其扩展(继承)activity,然后创建一个扩展broadcastreceiver 的内部类,不如直接继承broadcastreceiver。

关于android - 如何从通知中触发广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12372654/

相关文章:

android - Android中找到的GPS坐标的精度

android - 如何以编程方式更改 colors.xml 中颜色的值?

android - 如何仅显示当前月份并预览 datePickerDialog 中可选择的月份日期?

Android Vector Drawable 崩溃

安卓 : How to prevent users from entering numbers with more than 3 decimal places

android - 如何在 Android 中创建带圆角的 ListView?

android - 从 Google Photos App 获取视频(非本地)

java - 使用 Intent 从 ViewHolder 内部启动 Activity 会使应用程序崩溃

android - 使用 Intent.ACTION_GET_CONTENT 选择多个文件

java - 将动态数据存储到标签