【问题标题】:How to trigger a broadcast receiver from notification?如何从通知中触发广播接收器?
【发布时间】:2012-09-11 14:58:59
【问题描述】:

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

“无法实例化接收器 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>

【问题讨论】:

    标签: android android-layout android-intent


    【解决方案1】:

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

    而不是从活动扩展(继承),然后创建一个扩展广播接收器的内部类,然后直接从广播接收器继承。

    【讨论】:

    • 你能帮忙吗..当我按下这个通知按钮时,我想暂停音乐服务..你能指导我吗?接收广播接收器时如何访问服务方法
    【解决方案2】:

    在您的方法getDialogPendingIntent 中,您应该使用PendingIntent.getBroadcast 而不是PendingIntent.getActivity。这将导致您的PendingIntent 推送广播

    并解决这个问题:

    public class myBroad extends BroadcastReceiver

    【讨论】:

    • 在broadcastreciver创建扩展后它不起作用你能帮忙..当我按下这个通知按钮时我想暂停服务中的音乐..你能指导我吗?接收广播接收器时如何访问服务方法
    • 我怎样才能在广播接收器中识别它是按下暂停按钮还是播放按钮。同样,当我按下通知文本时,它不会让我进入活动..任何帮助
    • 首先决定,在选择通知时是要广播还是显示活动。其次,您可以在您的意图包中添加可选数据,以识别您希望在接收端执行的命令操作
    • 看到我想要两者,比如在通知上有媒体播放器控件,您可以在其中暂停播放音频,当您单击通知的文本部分时,您会重定向到活动.. 为了实现这一点,我使用广播接收器来是否按下按钮,根据我停止接收器的服务..我想在通知上使用 2 个按钮来暂停和播放..
    • 我已经用 2 个按钮的代码更新了主要问题,带有额外的意图和接收器,但在通知时我无法点击这 2 个按钮
    【解决方案3】:

    Exception 说得很清楚——你的 myBroad 必须扩展 BroadcastReceiver,而不是像现在这样扩展 Activity

    PS:您应该考虑切换到正确的命名约定。所以它应该是MyBroad(类)但myBroad(该类的对象)。即:

    MyBroad myBroad = new MyBroad();
    

    【讨论】:

    • 你能帮忙吗..当我按下这个通知按钮时,我想暂停音乐服务..你能指导我吗?接收广播接收器时如何访问服务方法
    • 我如何在广播接收器中识别出它的暂停按钮是按下还是播放按钮。同样,当我按下通知文本时,它不会让我进入活动..任何帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多