按下 .addAction() 时 BroadcastReceiver 未收到
BroadcastReceiver not receiving when .addAction() is pressed
我想在不打开应用程序的情况下关闭来自 .addAction()
的通知。问题是按下按钮时没有任何反应,onReceive()
方法不会触发。
这是 MainActivity 上的代码:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", SOMENUMBER);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(t);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SOMENUMBER, notification.build());
在其他 class 我有接收器:
public class Notification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
AndroidManifest.xml 文件的接收者:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="io.github.seik.Notification" />
</intent-filter>
</receiver>
您的命名约定令人困惑。 Android 已经有一个 class 叫做 Notification
,所以你可能不应该给你的接收器打电话 Notification
:-(
如果 MainActivity
扩展 Activity
那么您需要有一个清单条目,如下所示:
<activity android:name=".MainActivity"/>
对于您的 BroadcastReceiver
,您需要这样的清单条目:
<receiver android:name=".Notification"
android:exported="true"/>
由于您正在使用明确的 Intent
来启动您的 BroadcastReceiver
,因此您不需要为其提供 <intent-filter>
。由于 BroadcastReceiver
将由 NotificationManager
启动,您需要确保它是 exported
.
然后您需要创建 PendingIntent
以便它实际启动您的 BroadcastReceiver
,因此更改此:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
对此:
Intent notificationIntent = new Intent(mContext, Notification.class);
我想在不打开应用程序的情况下关闭来自 .addAction()
的通知。问题是按下按钮时没有任何反应,onReceive()
方法不会触发。
这是 MainActivity 上的代码:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", SOMENUMBER);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(t);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SOMENUMBER, notification.build());
在其他 class 我有接收器:
public class Notification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
AndroidManifest.xml 文件的接收者:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="io.github.seik.Notification" />
</intent-filter>
</receiver>
您的命名约定令人困惑。 Android 已经有一个 class 叫做 Notification
,所以你可能不应该给你的接收器打电话 Notification
:-(
如果 MainActivity
扩展 Activity
那么您需要有一个清单条目,如下所示:
<activity android:name=".MainActivity"/>
对于您的 BroadcastReceiver
,您需要这样的清单条目:
<receiver android:name=".Notification"
android:exported="true"/>
由于您正在使用明确的 Intent
来启动您的 BroadcastReceiver
,因此您不需要为其提供 <intent-filter>
。由于 BroadcastReceiver
将由 NotificationManager
启动,您需要确保它是 exported
.
然后您需要创建 PendingIntent
以便它实际启动您的 BroadcastReceiver
,因此更改此:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
对此:
Intent notificationIntent = new Intent(mContext, Notification.class);