Android 通知广播接收器未在活动中工作

Android 通知广播接收器未在活动中工作,android,android-intent,broadcastreceiver,android-notifications,Android,Android Intent,Broadcastreceiver,Android Notifications,我有一个通知,当按下通知中的特定按钮时,我正试图在活动中运行一个方法。我正在onCreate中创建broadcastReceiver: //broadcast receiver stuff broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Code

我有一个通知,当按下通知中的特定按钮时,我正试图在活动中运行一个方法。我正在onCreate中创建broadcastReceiver:

    //broadcast receiver stuff
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Code to run
            if (intent.getAction().equals("stop")){
                StartButtonClick();
            }
        }
    };
    registerReceiver(broadcastReceiver, new IntentFilter("stop"));
然后在同一活动中创建通知(但不是在onCreate中):

当我单击通知中的按钮时,什么也没有发生。我甚至在broadcastReceiver的if语句上加了一个断点,但按下按钮时什么也没发生。

试试这个

PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
而不是

PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);