我想分配一个在点击通知时将触发的事件,而不显示活动(如此处所述)。
我怎样才能做到这一点?
您可以通过注册广播接收器来做到这一点。这样,您可以在单击通知时在接收器中运行代码,而无需显示任何 UI。
基本接收器代码:
public class Provider extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//Your code here
}
}
在清单中:
<receiver android:name="Provider" />
通知的示例代码:
public static void showNotification(Context context) {
CharSequence title = "title";
CharSequence message = "text";
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(<image>, title, System.currentTimeMillis());
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent(context, Provider.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, pendingIntent);
notificationManager.notify(<notification id>, notification);
}