How to remove notification from notification bar programmatically in android?
Maybe try this :
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
OR, you can also do this to cancel all notifications in given context:
notificationManager.cancelAll();
See this link to the documentation : NotificationManager
Android: remove notification from notification bar
This is quite simple. You have to call cancel
or cancelAll
on your NotificationManager. The parameter of the cancel method is the ID of the notification that should be canceled.
See the API: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
How to remove a notification from the status bar
You can remove a notification using NotificationManager.cancel()
and passing in the id that was used in the notify
call.
notificationManager.cancel(100);
Remove all notifications from notification bar
Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService
, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.
First inside the onNotificationPosted
method you store all the StatusBarNotification
objects.
Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in onNotificationRemoved
method.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}
Finally you can remove any notification using cancelNotification
method.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
How to remove notification from status bar when clicking the my footer icon
You can call the notification outside service class (such as activity using button click) in this way:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(MY_NOTIFICATION_ID);
You can see the detail of it at https://developer.android.com/training/notify-user/build-notification#Removing.
Cancel (and hide) Android notification programmatically
If you want to use your Notification
as part of a foreground service, rather than manipulate flags directly, use startForeground()
and stopForeground()
on your Service
.
Remove notification bar android studio
On Android 4.0 and Lower
In AndroidManifest.xml -> inside the activity which you want to use, add the following to hide the status bar:
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
Programatically, by setting WindowManager flag:
Write a helper function
void hideStatusBar() {
// For Android version lower than Jellybean, use this call to hide the status bar.
if (Build.VERSION.SDK_INT < 16) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
Use the helper function in onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideStatusBar();
// We should never show the action bar if the status bar is hidden, so hide that too
//if necessary.
getSupportActionBar().hide() // if you have extended the activity from support lib like Appcompat, else use getActionBar().hide() here
setContentView(R.layout.activity_main);
}
Please Note:
- onCraete() will not get called always, so If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
- Use it before super.onCreate(savedInstanceState); if it crash
Find more details from here
Related Topics
Android: Keeping a Background Service Alive (Preventing Process Death)
Passing Arraylist Through Intent
Autocompletetextview Backed by Cursorloader
How to Create Jar for Android Library Project
Retrieve Incoming Call's Phone Number in Android
Bitmapfactory Oom Driving Me Nuts
Gridview with Two Columns and Auto Resized Images
Capture Screen Shot of Googlemap Android API V2
Callback to a Fragment from a Dialogfragment
Custom Filtering in Android Using Arrayadapter
Overlay Two Images in Android to Set an Imageview
How to Create Directory Automatically on Sd Card