【问题标题】:Notification in JellyBean - add action isn't workJellyBean 中的通知 - 添加操作不起作用
【发布时间】:2013-02-27 09:26:46
【问题描述】:

在使用自定义按钮(使用 addAction)构建 Jelly Bean 通知方面需要一些帮助。 问题是,我不能让它工作。 基本上,我想使用以下参数构建通知:在通知单击时,它必须启动我的一项活动,在按钮单击时 - 正在运行的播放暂停播放器,当显示通知时。 我的代码是: 接收者:

private BroadcastReceiver onNotification = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("NOTIFICATION", "Received broadcast");
        Bundle extras = intent.getExtras();
        int state = extras.getInt("state");
        if (state == 1) {
            playPauseMethod();
        }

    }

};

在我添加的活动的 onCreate 方法中:

IntentFilter notif_iff = new IntentFilter(
                    MainService.NOTIF_BROADCAST);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
                    onNotification, notif_iff);

MainService 中的代码如下:

public static final String ACTION = "com.formatbce.mdrive.action.UPDATE_UI";
public static final String NOTIF_BROADCAST = "com.formatbce.mdrive.action.NOTIF_BRD";
Intent in = new Intent(ACTION);
Intent notifInt = new Intent(NOTIF_BROADCAST);
private NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

private void showNotification(final int statusBarIconID,
        final int bigIconID, final int ppIconID, final String contentTitle,
        final String contentText, final boolean showIconOnly) {
    PendingIntent layoutIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MediaFragmentActivity.class), 0);

        int state = 1;
        notifInt.setAction("blabla");
        notifInt.putExtra("state", state);
        PendingIntent mIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 0, notifInt,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification = new Notification.Builder(this)
        .setContentTitle(contentTitle).setContentText(contentText)
        .setSmallIcon(bigIconID).setContentIntent(layoutIntent)
        .addAction(ppIconID, null, mIntent)
        .build();
            notification.flags |= Notification.FLAG_ONGOING_EVENT;
            mNM.notify(NOTIFICATION, notification);
}

所以,当我调用 showNotification() 方法时,会显示通知,然后单击它的正文效果很好。但是当我单击一个按钮时,使用 addAction 设置,没有任何变化......我的意思是,即使 onReceive() 中的那一行也不起作用:

Log.d("NOTIFICATION", "Received broadcast");

这里有什么问题?我大量阅读手册,但无法得到它。有人可以解释一下,如何让它工作? 谢谢!

附:当我将 addAction PendingIntent 更改为 getActivity 时,它工作正常,顺便说一句...

【问题讨论】:

  • 有人吗?请至少告诉我,我对该功能的理解是否存在问题,或者实际上没有人知道答案? :(

标签: android notifications broadcastreceiver broadcast android-4.2-jelly-bean


【解决方案1】:

我认为您在这里遇到的问题是 BroadcastReceiver 设置不正确。我实际上建议使用服务而不是广播接收器,只是由于广播接收器的延迟(同时,它应该很快发生,可能没什么大不了的)。

您的清单中的 BroadcastReceiver 设置是否正确?

您是否能够创建一个仅调用 BroadcastReceiver 的活动(以验证它是否正常工作)?

【讨论】:

  • 感谢您的回答。虽然这个项目已经被遗忘了,但我会试着在周末把它捡起来。
  • 我会说将广播接收器放在它自己的类中并在清单中注册它,然后尝试执行操作。我不确定(如果)内联广播接收器声明是如何向系统注册的。
  • 好吧,不得不说,在通常情况下,内联接收器就像一个魅力。但是,正如我所说,我会在周末试试这个,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2022-10-14
相关资源
最近更新 更多