代码之家  ›  专栏  ›  技术社区  ›  NullPointerException

如何在应用程序运行时(包括在后台)在状态栏中显示图标?

  •  26
  • NullPointerException  · 技术社区  · 14 年前

    我想在我的应用程序运行时,包括在后台运行时,在状态栏中放置一个图标。我该怎么做?

    4 回复  |  直到 8 年前
        1
  •  19
  •   Ciro Santilli OurBigBook.com    8 年前

    您应该可以使用通知和通知管理器来执行此操作。然而,获得一种确定的方法来知道应用程序何时不运行是困难的。

    您可以通过执行以下操作来获得所需的基本功能:

    Notification notification = new Notification(R.drawable.your_app_icon,
                                                 R.string.name_of_your_app, 
                                                 System.currentTimeMillis());
    notification.flags |= Notification.FLAG_NO_CLEAR
                       | Notification.FLAG_ONGOING_EVENT;
    NotificationManager notifier = (NotificationManager)
         context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifier.notify(1, notification);
    

    这段代码必须在应用程序启动时确定会被触发的地方。可能在应用程序的自定义应用程序对象的onCreate()方法中。

    ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
    

    将需要什么来删除图标。

        2
  •  9
  •   mihirjoshi    9 年前

    NotificationCompat.Builder -

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Title");
    Intent resultIntent = new Intent(this, MyActivity.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    
    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(NOTIFICATION_ID, notification);
    

    mNotifyMgr.cancel(NOTIFICATION_ID);
    
        3
  •  6
  •   Brian    14 年前

    看看开发指南” Creating Status Bar Notifications ".

    onCreate() 打电话来 cancel(int) 在你的 onPause() isFinishing() 返回true。

    private static final int NOTIFICATION_EX = 1;
    private NotificationManager notificationManager;
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        notificationManager = (NotificationManager) 
            getSystemService(Context.NOTIFICATION_SERVICE);
    
        int icon = R.drawable.notification_icon;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
    
        Notification notification = new Notification(icon, tickerText, when);
    
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this, MyClass.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 
            0, notificationIntent, 0);
    
        notification.setLatestEventInfo(context, contentTitle, 
            contentText, contentIntent);
    
        notificationManager.notify(NOTIFICATION_EX, notification);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            notificationManager.cancel(NOTIFICATION_EX);
        }
    }
    
        4
  •  4
  •   Luiz Lima    8 年前

    真的很管用。 我在上面的例子中创建了一个方法:

    private void applyStatusBar(String iconTitle, int notificationId) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(iconTitle);
    Intent resultIntent = new Intent(this, ActMain.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;
    
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(notificationId, notification);}