Android:新通知到达时显示/隐藏状态栏

时间:2012-09-07 04:22:05

标签: android android-notifications android-notification-bar

我的应用程序应该以全屏模式运行。当新项目从我们的服务器到达我的应用程序时,我还必须显示通知栏。我在BroadcastReceiver类中编写通知编码。我想在MainActivity中做出决定,例如仅在收到新通知时显示状态栏,否则隐藏状态栏。所有这些都是在应用程序处于前台时完成的。

通知编码:

Intent scheduledIntent = new Intent(context,Activity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, scheduledIntent, 0);

                nm = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                CharSequence from = "From address";
                CharSequence message = "New Orders received";
                Notification notif = new Notification(R.drawable.icon,"Hai", System.currentTimeMillis());
                notif.setLatestEventInfo(context, from, message, pendingIntent);
                notif.flags |= Notification.FLAG_AUTO_CANCEL;
                nm.notify(uniqueID, notif);
                notif.ledOnMS = 500;
                notif.ledOffMS = 500;

通过此代码收到通知。但状态栏始终可见。收到通知时我需要隐藏。 请告诉我正确的方法。

提前致谢,

1 个答案:

答案 0 :(得分:0)

在OnCreate()方法中,您必须编写以下内容:

 AppPreferences prefs = new AppPreferences(PresentChatActivity.this);
             boolean notify_checkbox = prefs.getNotifyState();

    Notification(message.getBody(),fromName, name);

然后,在OnCreate()之外,编写此函数

   protected void Notification(String body, String fromName, String Notificationmsg) {

    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);      

    String tickerText = getString(R.string.app_name, body);
    Notification notif = new Notification(R.drawable.ic_launcher, tickerText,
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, PresentChatActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


     notif.setLatestEventInfo(PresentChatActivity.this,Notificationmsg, message, pendingIntent);
     notif.setLatestEventInfo(getApplicationContext(), fromName, body,pendingIntent );  
     notificationManager.notify(10001, notif);
     }