0

我正在开发一个 Android 应用程序。该应用程序就像本机短信应用程序。我在通知部分遇到问题。在本机应用程序中,它们以下列方式处理通知。

1)如果是来自特定号码的消息,则点击通知将进入相应联系人的聊天页面,通知将被清除。

2)如果来自不同号码的消息,那么点击通知将导致主页,通知不会清除。

我已经完成了第一部分,但我不知道要做第二部分。有没有办法让通知部分不明确并调用意图。?

4

2 回答 2

6

你必须添加一个标志:

 notification.flags |= Notification.FLAG_ONGOING_EVENT;

这是一个完整的例子:

 private static final int NOTIFICATION_EX = 0;
 private NotificationManager notificationManager;

...

在 onCreate 中:

    notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);



int icon = R.drawable.youricon;
    CharSequence tickerText = "Sticky notification";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "Sticky notification";
    CharSequence contentText = "...click here and it wont go away...";
    Intent notificationIntent = new Intent(this, mainmenu.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, 
        contentText, contentIntent);
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    notificationManager.notify(NOTIFICATION_EX, notification);

您可以根据自己的喜好调整意图。

于 2012-09-25T12:01:54.937 回答
1

您将必须创建两种不同类型的通知,第一种您已经实现,第二种您可以复制第一个并检查数字是否不同,使用第二个通知并将标志设置为不清除notification2.flags |= Notification.FLAG_ONGOING_EVENT;单击其中的 HomeActivityPendingIntentIntent

于 2012-09-25T12:03:49.973 回答