通知 BroadcastReceiver 在 Activity 中不工作
Notification BroadcastReceiver Not Working In Activity
我有一个通知,当按下通知中的特定按钮时,我正在尝试 运行 activity 中的方法。我在 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"));
然后我在同一个 activity 中创建通知(虽然不在 onCreate 中):
private void ShowNotification(){
//add intent for app to resume when notification is clicked
PendingIntent reopenAppIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//add intent for app to stop service when stop button in notification is clicked
//this is the intent I am creating for the notification button
Intent stopIntent = new Intent("stop");
PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//create notification
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setContentTitle("Messaging in progress")
.setContentText("User is late")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_message)
.setContentIntent(reopenAppIntent)
.addAction(R.drawable.ic_message, "Stop", stopPendingIntent); //here I am adding the intent for the butt that I want to activate the broadcastReceiver
notificationManager.notify(1, notification.build());
}
当我点击通知中的按钮时,没有任何反应。我什至在 broadcastReceiver 的 if 语句上设置了一个断点,按下按钮时什么也没有发生。
试试这个
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
而不是
PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我有一个通知,当按下通知中的特定按钮时,我正在尝试 运行 activity 中的方法。我在 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"));
然后我在同一个 activity 中创建通知(虽然不在 onCreate 中):
private void ShowNotification(){
//add intent for app to resume when notification is clicked
PendingIntent reopenAppIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//add intent for app to stop service when stop button in notification is clicked
//this is the intent I am creating for the notification button
Intent stopIntent = new Intent("stop");
PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//create notification
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setContentTitle("Messaging in progress")
.setContentText("User is late")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_message)
.setContentIntent(reopenAppIntent)
.addAction(R.drawable.ic_message, "Stop", stopPendingIntent); //here I am adding the intent for the butt that I want to activate the broadcastReceiver
notificationManager.notify(1, notification.build());
}
当我点击通知中的按钮时,没有任何反应。我什至在 broadcastReceiver 的 if 语句上设置了一个断点,按下按钮时什么也没有发生。
试试这个
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
而不是
PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);