0

我的应用程序应该以全屏模式运行。当新项目从我们的服务器到达我的应用程序时,我还必须显示通知栏。我在 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;

通过此代码收到通知。但状态栏始终可见。没有收到通知时我需要隐藏。请为我提供正确的方法来做到这一点。

提前致谢,

4

1 回答 1

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);
     }
于 2012-09-07T04:29:45.043 回答