2016-07-31 73 views
3

如何在应用程序中隐藏持久通知?隐藏持久通知

目前我的通知它也显示在锁屏中,但我不想。我只想在通知菜单上滑动时显示通知。

我知道这是可能的,因为我有一个名为WiFi ADB的应用程序,它正是这样做的。 (Android版的TouchWiz 5.0.1)

这是我通知的代码(它是一个服务):

@Override 
public int onStartCommand(Intent intent, int _flags, int _startId) { 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    noti = new Notification.Builder(getApplicationContext()) 
      .setContentTitle("Title") 
      .setContentText("Subtitle") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(contentIntent) 
      .build(); 
    startForeground(1234, noti); 
    mLocalBroadcastManager.sendBroadcast(new Intent(SERVICE_STARTED)); 
    return Service.START_STICKY; 
} 
+0

的可能的复制【如何抑制Android的5锁定屏幕上的通知(棒棒堂),但让它在通知区域?](http://stackoverflow.com/questions/27035202/how-to-suppress-notification-on-lock-screen-in-android-5-lollipop-but-let-it-我) –

回答

2

使用setVisibility(NotificationCompat.VISIBILITY_SECRET) & setPriority(Notification.PRIORITY_MIN)

mNotification = new Notification.Builder(getApplicationContext()) 
     .setContentTitle("Title") 
     .setContentText("Subtitle") 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentIntent(contentIntent) 
     .setOngoing(true) 
     .setVisibility(NotificationCompat.VISIBILITY_SECRET) 
     .setPriority(Notification.PRIORITY_MIN) 
     .build(); 

对于VISIBILITY_SECRET

通知可见性:不透露任何部分不通过安全的锁屏。

对于PRIORITY_MIN

最低优先级;除特殊情况外,这些项目可能不会显示给用户,例如详细的通知日志。


如果你想仍然显示在状态栏上的图标,对于没有选择,但你可以通过订阅USER_PRESENT & SCREEN_OFF事件构建一个简单的解决方法:

  • 取消当您收到通知时SCREEN_OFF事件
  • 当您收到时重新通知USER_PRESENT

寄存器BroadcastReceiver &通知默认的通知:

mNotificationmanager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

IntentFilter filter = new IntentFilter(); 
filter.addAction("android.intent.action.USER_PRESENT"); 
filter.addAction("android.intent.action.SCREEN_OFF"); 
registerReceiver(mBroadcastReceiver, filter); 

Intent notificationIntent = new Intent(this, YourActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

mNotification = new Notification.Builder(getApplicationContext()) 
     .setContentTitle("Title") 
     .setContentText("Subtitle") 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentIntent(contentIntent) 
     .setOngoing(true) 
     .build(); 
mNotificationmanager.notify(NOTIF_ID, mNotification); 

BroadcastReceiver取消SCREEN_OFF,通知上USER_PRESENT

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.v("test", intent.getAction()); 

     if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) { 
      mNotificationmanager.cancel(NOTIF_ID); 
     } else if (intent.getAction().equals("android.intent.action.USER_PRESENT")) { 
      mNotificationmanager.notify(NOTIF_ID, mNotification); 
     } 

    } 
}; 
+0

比你呢!这是工作!但是有没有办法在状态栏中显示图标? – Fr0z3n

+0

如果您想要在状态栏中显示通知图标,在通知菜单中没有显示通知菜单中的通知记录,我已经使用替代解决方案进行了更新 –