2016-07-22 18 views
2

在我的应用程序中,我添加了一个正在进行的通知。通知在状态栏上有一个图标,在通知区域有更多详细信息。Android - 如何将状态栏上的进行中的事件,但不通知屏幕上?

此图片来自Google。这是在通知区域:

This image is from Google. This is the notification area

我需要在状态栏上的图标会在那里住总,但用户将能够解雇通知屏幕的通知,以便用户将与只停留状态栏上的图标。 我该怎么办?

我的服务代码:

@SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, START_STICKY, startId); 

     ... 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.myNotificationString)) 
      .setContentText(string1 + " | " + string2) 
      .setLargeIcon(appIcon) 
      .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName())); // the icon changes every day 

     Intent resultIntent = new Intent(this, MainActivity.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(myRequestCode, notification); 

     return START_STICKY; 
    } 
+0

抱歉,但我目前还不清楚我编辑的问题 – ddb

+0

。简而言之:假设我有一个应用程序向用户显示电池状态。用户希望在状态栏上看到电池状态,但他不想将其始终视为通知区域中的通知。所以我希望用户能够关闭通知区域中的通知,但状态栏上的图标不会消失。 – TamarG

+0

像闹钟主动通知?您可以删除已设置闹钟的通知,但如果您将其删除,您总是会在状态栏中看到应用程序图标。我明白了吗? – ddb

回答

0

尝试定义resultPendingIntent像下面

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_ONGOING_EVENT); 

编辑1

@SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, START_STICKY, startId); 

     ... 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.myNotificationString)) 
      .setContentText(string1 + " | " + string2) 
      .setLargeIcon(appIcon) 
      .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName())); // the icon changes every day 

     Intent resultIntent = new Intent(this, MainActivity.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(myRequestCode, notification); 

     return START_STICKY; 
    } 
+0

但我有“\t notification.flags | = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;” - 这不够吗?无论如何,它不起作用,因为用户不能解雇通知,它是粘性的。 – TamarG

+0

好的,我明白了。然后恢复该更改并进行此更改'notification.flags = Notification.FLAG_ONGOING_EVENT;' – ddb

+0

对不起,试试这个'notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;',所以去掉'|'在分配新建分配FY – ddb

相关问题