首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:新通知到达时显示/隐藏状态栏

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

Stack Overflow用户
提问于 2012-09-07 12:22:05
回答 1查看 1.8K关注 0票数 0

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

通知编码:

代码语言:javascript
运行
复制
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;

通过此代码接收通知。但状态栏始终可见。我需要在没有收到通知的时候隐藏起来。请告诉我做这件事的正确方法。

提前谢谢你,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-07 12:29:45

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

代码语言:javascript
运行
复制
 AppPreferences prefs = new AppPreferences(PresentChatActivity.this);
             boolean notify_checkbox = prefs.getNotifyState();

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

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

代码语言:javascript
运行
复制
   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);
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12311690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档