List of usage examples for android.app NotificationManager cancel
public void cancel(int id)
From source file:de.mangelow.throughput.NotificationService.java
private void removeNotification() { NotificationManager nmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nmanager.cancel(NOTIFICATION_ID); if (mResultReceiver != null) { Bundle bundle = new Bundle(); bundle.putInt("drawable", R.drawable.ic_stat_zero); bundle.putString("title", ""); bundle.putString("subtitle", ""); mResultReceiver.send(0, bundle); }/*ww w .j a va2s. c o m*/ }
From source file:de.ub0r.android.smsdroid.SmsReceiver.java
/** * Update new message {@link Notification}. * * @param context {@link Context}/*from w ww .j a va 2 s . c o m*/ * @param text text of the last assumed unread message * @return number of unread messages */ static int updateNewMessageNotification(final Context context, final String text) { Log.d(TAG, "updNewMsgNoti(", context, ",", text, ")"); final NotificationManager mNotificationMgr = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final boolean enableNotifications = prefs.getBoolean(PreferencesActivity.PREFS_NOTIFICATION_ENABLE, true); final boolean privateNotification = prefs.getBoolean(PreferencesActivity.PREFS_NOTIFICATION_PRIVACY, false); final boolean showPhoto = !privateNotification && prefs.getBoolean(PreferencesActivity.PREFS_CONTACT_PHOTO, true); if (!enableNotifications) { mNotificationMgr.cancelAll(); Log.d(TAG, "no notification needed!"); } final int[] status = getUnread(context.getContentResolver(), text); final int l = status[ID_COUNT]; final int tid = status[ID_TID]; // FIXME l is always -1.. Log.d(TAG, "l: ", l); if (l < 0) { return l; } if (enableNotifications && (text != null || l == 0)) { mNotificationMgr.cancel(NOTIFICATION_ID_NEW); } Uri uri; PendingIntent pIntent; if (l == 0) { final Intent i = new Intent(context, ConversationListActivity.class); // add pending intent i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); } else { final NotificationCompat.Builder nb = new NotificationCompat.Builder(context); boolean showNotification = true; Intent i; if (tid >= 0) { uri = Uri.parse(MessageListActivity.URI + tid); i = new Intent(Intent.ACTION_VIEW, uri, context, MessageListActivity.class); pIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); if (enableNotifications) { final Conversation conv = Conversation.getConversation(context, tid, true); if (conv != null) { String a; if (privateNotification) { if (l == 1) { a = context.getString(R.string.new_message_); } else { a = context.getString(R.string.new_messages_); } } else { a = conv.getContact().getDisplayName(); } showNotification = true; nb.setSmallIcon(PreferencesActivity.getNotificationIcon(context)); nb.setTicker(a); nb.setWhen(lastUnreadDate); if (l == 1) { String body; if (privateNotification) { body = context.getString(R.string.new_message); } else { body = lastUnreadBody; } if (body == null) { body = context.getString(R.string.mms_conversation); } nb.setContentTitle(a); nb.setContentText(body); nb.setContentIntent(pIntent); // add long text nb.setStyle(new NotificationCompat.BigTextStyle().bigText(body)); // add actions Intent nextIntent = new Intent(NotificationBroadcastReceiver.ACTION_MARK_READ); nextIntent.putExtra(NotificationBroadcastReceiver.EXTRA_MURI, uri.toString()); PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT); nb.addAction(R.drawable.ic_menu_mark, context.getString(R.string.mark_read_), nextPendingIntent); nb.addAction(R.drawable.ic_menu_compose, context.getString(R.string.reply), pIntent); } else { nb.setContentTitle(a); nb.setContentText(context.getString(R.string.new_messages, l)); nb.setContentIntent(pIntent); } if (showPhoto // just for the speeeeed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { try { conv.getContact().update(context, false, true); } catch (NullPointerException e) { Log.e(TAG, "updating contact failed", e); } Drawable d = conv.getContact().getAvatar(context, null); if (d instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); // 24x24 dp according to android iconography -> // http://developer.android.com/design/style/iconography.html#notification int px = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 64, context.getResources().getDisplayMetrics())); nb.setLargeIcon(Bitmap.createScaledBitmap(bitmap, px, px, false)); } } } } } else { uri = Uri.parse(MessageListActivity.URI); i = new Intent(Intent.ACTION_VIEW, uri, context, ConversationListActivity.class); pIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); if (enableNotifications) { showNotification = true; nb.setSmallIcon(PreferencesActivity.getNotificationIcon(context)); nb.setTicker(context.getString(R.string.new_messages_)); nb.setWhen(lastUnreadDate); nb.setContentTitle(context.getString(R.string.new_messages_)); nb.setContentText(context.getString(R.string.new_messages, l)); nb.setContentIntent(pIntent); nb.setNumber(l); } } // add pending intent i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); if (enableNotifications && showNotification) { int[] ledFlash = PreferencesActivity.getLEDflash(context); nb.setLights(PreferencesActivity.getLEDcolor(context), ledFlash[0], ledFlash[1]); final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context); if (text != null) { final boolean vibrate = p.getBoolean(PreferencesActivity.PREFS_VIBRATE, false); final String s = p.getString(PreferencesActivity.PREFS_SOUND, null); Uri sound; if (s == null || s.length() <= 0) { sound = null; } else { sound = Uri.parse(s); } if (vibrate) { final long[] pattern = PreferencesActivity.getVibratorPattern(context); if (pattern.length == 1 && pattern[0] == 0) { nb.setDefaults(Notification.DEFAULT_VIBRATE); } else { nb.setVibrate(pattern); } } nb.setSound(sound); } } Log.d(TAG, "uri: ", uri); mNotificationMgr.cancel(NOTIFICATION_ID_NEW); if (enableNotifications && showNotification) { try { mNotificationMgr.notify(NOTIFICATION_ID_NEW, nb.getNotification()); } catch (IllegalArgumentException e) { Log.e(TAG, "illegal notification: ", nb, e); } } } Log.d(TAG, "return ", l, " (2)"); //noinspection ConstantConditions AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetProvider.class), WidgetProvider.getRemoteViews(context, l, pIntent)); return l; }
From source file:com.commonsware.android.hcnotify.SillyService.java
@Override protected void onHandleIntent(Intent intent) { NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContent(buildContent(0)).setTicker(getText(R.string.ticker), buildTicker()) .setContentIntent(buildContentIntent()).setLargeIcon(buildLargeIcon()) .setSmallIcon(R.drawable.ic_stat_notif_small_icon).setOngoing(true); Notification notif = builder.build(); for (int i = 0; i < 20; i++) { notif.contentView.setProgressBar(android.R.id.progress, 100, i * 5, false); mgr.notify(NOTIFICATION_ID, notif); if (i == 0) { notif.tickerText = null;//from w w w.j a v a2 s. co m notif.tickerView = null; } SystemClock.sleep(1000); } mgr.cancel(NOTIFICATION_ID); }
From source file:ca.spencerelliott.mercury.Changesets.java
@Override public void onCreate(Bundle bundle) { super.onCreate(bundle); this.requestWindowFeature(Window.FEATURE_PROGRESS); this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.changesets); //Cancel any notifications previously setup NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(1); //Create a new array list for the changesets changesets_list = new ArrayList<Map<String, ?>>(); changesets_data = new ArrayList<Beans.ChangesetBean>(); //Get the list view to store the changesets changesets_listview = (ListView) findViewById(R.id.changesets_list); TextView empty_text = (TextView) findViewById(R.id.changesets_empty_text); //Set the empty view changesets_listview.setEmptyView(empty_text); //Use a simple adapter to display the changesets based on the array list made earlier changesets_listview.setAdapter(new SimpleAdapter(this, changesets_list, R.layout.changeset_item, new String[] { COMMIT, FORMATTED_INFO }, new int[] { R.id.changesets_commit, R.id.changesets_info })); //Set the on click listener changesets_listview.setOnItemClickListener(new OnItemClickListener() { @Override/* w w w .jav a2s.c om*/ public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) { Intent intent = new Intent(Changesets.this, ChangesetViewer.class); //Pass the changeset information to the changeset viewer Bundle params = new Bundle(); params.putString("changeset_commit_text", changesets_data.get(position).getTitle()); params.putString("changeset_changes", changesets_data.get(position).getContent()); params.putLong("changeset_updated", changesets_data.get(position).getUpdated()); params.putString("changeset_authors", changesets_data.get(position).getAuthor()); params.putString("changeset_link", changesets_data.get(position).getLink()); //params.putBoolean("is_https", is_https); intent.putExtras(params); startActivity(intent); } }); //Register the list view for opening the context menu registerForContextMenu(changesets_listview); //Get the intent passed by the program if (getIntent() != null) { //Check to see if this is a search window if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { //Change the title of the activity and the empty text of the list so it looks like a search window this.setTitle(R.string.search_results_label); empty_text.setText(R.string.search_results_empty); //Retrieve the query the user entered String query = getIntent().getStringExtra(SearchManager.QUERY); //Convert the query to lower case query = query.toLowerCase(); //Retrieve the bundle data Bundle retrieved_data = getIntent().getBundleExtra(SearchManager.APP_DATA); //If the bundle was passed, grab the changeset data if (retrieved_data != null) { changesets_data = retrieved_data .getParcelableArrayList("ca.spencerelliott.mercury.SEARCH_DATA"); } //If we're missing changeset data, stop here if (changesets_data == null) return; //Create a new array list to store the changesets that were a match ArrayList<Beans.ChangesetBean> search_beans = new ArrayList<Beans.ChangesetBean>(); //Loop through each changeset for (Beans.ChangesetBean b : changesets_data) { //Check to see if any changesets match if (b.getTitle().toLowerCase().contains(query)) { //Get the title and date of the commit String commit_text = b.getTitle(); Date commit_date = new Date(b.getUpdated()); //Add a new changeset to display in the list view changesets_list.add(createChangeset( (commit_text.length() > 30 ? commit_text.substring(0, 30) + "..." : commit_text), b.getRevisionID() + " - " + commit_date.toLocaleString())); //Add this bean to the list of found search beans search_beans.add(b); } } //Switch the changeset data over to the changeset data that was a match changesets_data = search_beans; //Update the list in the activity list_handler.sendEmptyMessage(SUCCESSFUL); //Notify the activity that it is a search window is_search_window = true; //Stop loading here return; } //Get the data from the intent Uri data = getIntent().getData(); if (data != null) { //Extract the path in the intent String path_string = data.getEncodedPath(); //Split it by the forward slashes String[] split_path = path_string.split("/"); //Make sure a valid path was passed if (split_path.length == 3) { //Get the repository id from the intent repo_id = Long.parseLong(split_path[2].toString()); } else { //Notify the user if there was a problem Toast.makeText(this, R.string.invalid_intent, 1000).show(); } } } //Retrieve the changesets refreshChangesets(); }
From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuActivity.java
private void updateProgressBar(final int progress, final boolean error) { switch (progress) { case DfuService.PROGRESS_CONNECTING: mProgressBar.setIndeterminate(true); mTextPercentage.setText(R.string.dfu_status_connecting); break;//from w w w .jav a 2s .c om case DfuService.PROGRESS_STARTING: mProgressBar.setIndeterminate(true); mTextPercentage.setText(R.string.dfu_status_starting); break; case DfuService.PROGRESS_VALIDATING: mProgressBar.setIndeterminate(true); mTextPercentage.setText(R.string.dfu_status_validating); break; case DfuService.PROGRESS_DISCONNECTING: mProgressBar.setIndeterminate(true); mTextPercentage.setText(R.string.dfu_status_disconnecting); break; case DfuService.PROGRESS_COMPLETED: mTextPercentage.setText(R.string.dfu_status_completed); // let's wait a bit until we reconnect to the device again. Mainly because of the notification. When canceled immediately it will be recreated by service again. new Handler().postDelayed(new Runnable() { @Override public void run() { showFileTransferSuccessMessage(); // if this activity is still open and upload process was completed, cancel the notification final NotificationManager manager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); manager.cancel(DfuService.NOTIFICATION_ID); } }, 200); break; default: mProgressBar.setIndeterminate(false); if (error) { showErrorMessage(progress); } else { mProgressBar.setProgress(progress); mTextPercentage.setText(getString(R.string.progress, progress)); } break; } }
From source file:com.klinker.deskclock.alarms.AlarmNotifications.java
public static void showHighPriorityNotification(Context context, AlarmInstance instance) { Log.v("Displaying high priority notification for alarm instance: " + instance.mId); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Resources resources = context.getResources(); NotificationCompat.Builder notification = new NotificationCompat.Builder(context) .setContentTitle(resources.getString(R.string.alarm_alert_predismiss_title)) .setContentText(AlarmUtils.getAlarmText(context, instance)) .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false) .setPriority(NotificationCompat.PRIORITY_HIGH); // Setup up dismiss action Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance, AlarmInstance.DISMISSED_STATE); notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, resources.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // Setup content action if instance is owned by alarm long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId; Intent viewAlarmIntent = Alarm.createIntent(context, DeskClock.class, alarmId); viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX); viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId); viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT)); nm.cancel(instance.hashCode()); nm.notify(instance.hashCode(), notification.build()); }
From source file:org.gluu.super_gluu.app.activities.MainNavDrawerActivity.java
public void doQrRequest(OxPush2Request oxPush2Request) { if (!validateOxPush2Request(oxPush2Request)) { return;/*from ww w . j a v a 2 s. com*/ } NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); if (notificationManager != null) { notificationManager.cancel(MainNavDrawerActivity.MESSAGE_NOTIFICATION_ID); } final ProcessManager processManager = createProcessManager(oxPush2Request); RequestProcessListener requestProcessListener = new RequestProcessListener() { @Override public void onApprove() { String licenseId = OxPush2Request.getLicenseId(oxPush2Request.getIssuer(), oxPush2Request.getUserName()); Settings.updateLicense(context, licenseId, oxPush2Request.isLicensed()); Settings.clearPushOxData(getApplicationContext()); processManager.onOxPushRequest(false); } @Override public void onDeny() { Settings.clearPushOxData(getApplicationContext()); processManager.onOxPushRequest(true); } }; RequestDetailFragment requestDetailFragment = RequestDetailFragment.newInstance(false, null, oxPush2Request, requestProcessListener); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_frame_layout, requestDetailFragment); transaction.addToBackStack(null); transaction.commitAllowingStateLoss(); }
From source file:com.deepak.myclock.alarms.AlarmNotifications.java
public static void showHighPriorityNotification(Context context, AlarmInstance instance) { Log.v("Displaying high priority notification for alarm instance: " + instance.mId); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Resources resources = context.getResources(); NotificationCompat.Builder notification = new NotificationCompat.Builder(context) .setContentTitle(resources.getString(R.string.alarm_alert_predismiss_title)) .setContentText(AlarmUtils.getAlarmText(context, instance)) .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false) .setPriority(NotificationCompat.PRIORITY_HIGH); // Setup up dismiss action Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance, AlarmInstance.DISMISSED_STATE); notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, resources.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // Setup content action if instance is owned by alarm long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId; Intent viewAlarmIntent = Alarm.createIntent(context, MainActivity.class, alarmId); //viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX); viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId); viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT)); nm.cancel(instance.hashCode()); nm.notify(instance.hashCode(), notification.build()); }
From source file:de.yaacc.player.AbstractPlayer.java
/** * Cancels the notification./*from w w w .jav a2 s. co m*/ */ private void cancleNotification() { NotificationManager mNotificationManager = (NotificationManager) getContext() .getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. Log.d(getClass().getName(), "Cancle Notification with ID: " + getNotificationId()); mNotificationManager.cancel(getNotificationId()); }
From source file:de.linuxwhatelse.android.notify.activities.MainActivity.java
private void handleSnoozedNotification() { this.initChannels(this, getString(R.string.notification_channel_snooze)); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); boolean notificationsSnoozed = preferences.getBoolean(Notify.PREFERENCE_KEY_NOTIFICATIONS_SNOOZED, false); boolean eventsSnoozed = preferences.getBoolean(Notify.PREFERENCE_KEY_EVENTS_SNOOZED, false); long snoozeUntil = preferences.getLong(Notify.PREFERENCE_KEY_SNOOZED_UNTIL, -1); if (!notificationsSnoozed && !eventsSnoozed) { notificationManager.cancel(Notify.NOTIFICATION_ID_SNOOZE); return;/*from w w w .j ava 2 s . co m*/ } String title = getString(R.string.snoozed_notifications_title); if (notificationsSnoozed && eventsSnoozed) { title = getString(R.string.snoozed_all_title); } String body = getString(R.string.snoozed_text_resume_indefinitely); if (snoozeUntil != -1) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(snoozeUntil); String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()); String time = (new SimpleDateFormat("HH:mm", getResources().getConfiguration().locale)) .format(cal.getTime()); body = getString(R.string.snoozed_text_resume_at) + " " + day + " " + time; } Intent activityIntent = new Intent(context, MainActivity.class); activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Notify.NOTIFICATION_CHANNEL) .setContentTitle(title).setContentText(body).setSmallIcon(R.drawable.ic_notifications_paused) .setPriority(NotificationCompat.PRIORITY_LOW).setContentIntent(activityPendingIntent) .setOngoing(true); Intent intent = new Intent(getApplicationContext(), SnoozeEndReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_notifications_active, getString(R.string.snoozed_action_resume), pendingIntent).build(); mBuilder.addAction(action); notificationManager.notify(Notify.NOTIFICATION_ID_SNOOZE, mBuilder.build()); }