通知栏上显示android通知

时间:2015-01-28 03:37:27

标签: android notifications

我想通过Android系统听取nofication显示事件,这意味着我想知道手机上的通知栏上是否显示了nofication,我该如何获得目标?感谢。

1 个答案:

答案 0 :(得分:0)

以下是通知通知的代码

private void generateNotification(Context context) {

            long when = System.currentTimeMillis();
            // notifytime = when;
            Intent notifyIntent = new Intent(context,
                    com.example.RegisterActivity.class);

            notifyIntent.putExtra("address", "Hello World");

            notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Hello World").setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND).setWhen(when);

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            builder.setAutoCancel(true);
            notificationManager.notify((int) when, builder.build());
        }

我们可以放置通知ID,我们可以在NotificationManager中检查通知ID