Her*_*oma 1 android android-notifications android-notification-bar
我正在努力在 Android 中构建本地通知。我设法在状态栏中显示了一个本地通知,但我无法使用setAutoCancel()或任何方式隐藏它。这是我的代码:
我的用例是:我向用户显示一个本地通知,表明我正在做一些处理。完成处理后,我会更改通知上的文本
NotificationManager manager = NotificationManagerCompat.from( this );
NotificationBuilder builder = new NotificationCompat.Builder( getApplicationContext() );
builder.setOngoing( true )
.setSmallIcon( R.drawable.X )
.setContentTitle("Content title")
.setPriority( NotificationCompat.PRIORITY_MAX )
.setProgress( 100, 0, false );
Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
manager.notify( 100, notification );
Run Code Online (Sandbox Code Playgroud)
完成一些处理后,我希望能够清除通知,因此我执行以下操作:
PendingIntent btPendingIntent = PendingIntent.getActivity( this, 0, new Intent( ), 0 );
NotificationCompat.Builder mb = new NotificationCompat.Builder(this);
mb.setSmallIcon( R.drawable.ic_action_X )
.setLargeIcon( bitmap )
.setContentTitle("Upload complete")
.setAutoCancel( true );
mb.setContentIntent( btPendingIntent );
notificationManager.notify( 100, mb.build() );
Run Code Online (Sandbox Code Playgroud)
通知会根据我的需要更改文本和图标,但是当用户点击通知时没有任何反应 - 通知不会被取消。
有没有办法对取消通知但不打开任何活动或其他任何内容的通知执行操作?
您可以使用NotificationManager.cancel()并传入notify呼叫中使用的 id来删除通知。
notificationManager.cancel(100);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2731 次 |
| 最近记录: |