Example usage for android.app NotificationManager cancelAll

List of usage examples for android.app NotificationManager cancelAll

Introduction

In this page you can find the example usage for android.app NotificationManager cancelAll.

Prototype

public void cancelAll() 

Source Link

Document

Cancel all previously shown notifications.

Usage

From source file:com.mathi_amorim.emmanuel.metrictime.UpdateTimeService.java

private void updateNotification() {
    if (Config.showNotification) {
        MetricTime time = MetricTimeConverter.currentMetricTime();
        String currentTime = String.format("%1$01d:%2$02d", time.hours, time.minutes);

        Intent intent = new Intent(this, SettingsActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setContentTitle("Metric Time");
        mBuilder.setContentText(currentTime);
        mBuilder.setOngoing(true);//from  ww w . j  av a  2 s .c  o  m
        mBuilder.setContentIntent(pendingIntent);

        int mNotificationId = 1;
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    } else {
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.cancelAll();
    }
}

From source file:com.brayanarias.alarmproject.activity.AlarmScreenActivity.java

@Override
protected void onDestroy() {
    Log.e(TAG, "on Destroy method");
    if (task != null) {
        task.cancel(true);//w  w  w  .ja va2  s  . c om
    }
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, actualVolume, 0);
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
    if (vibrator != null) {
        vibrator.cancel();
    }
    NotificationManager alarmNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    alarmNotificationManager.cancelAll();
    if (flag) {
        int deleteAlarm = getIntent().getIntExtra(Constant.alarmDeleteAfterKey, 0);
        int id = getIntent().getIntExtra(Constant.alarmIdKey, -1);

        if (deleteAlarm == 1) {
            DataBaseManager dataBaseManager = DataBaseManager.getInstance(getApplicationContext());
            AlarmDataBase.deleteAlarm(id, dataBaseManager);
        } else if (getIntent().getStringExtra(Constant.alarmDaysKey).equals("0000000")) {
            DataBaseManager dataBaseManager = DataBaseManager.getInstance(getApplicationContext());
            Alarm alarm = AlarmDataBase.getAlarmById(dataBaseManager, id);
            alarm.setActivated(0);
            AlarmDataBase.updateAlarm(alarm, dataBaseManager);
        }
        AlarmReceiver.setAlarms(getApplicationContext());
    }
    super.onDestroy();
}

From source file:de.ub0r.android.smsdroid.SmsReceiver.java

/**
 * Update new message {@link Notification}.
 *
 * @param context {@link Context}/*from  ww  w  .jav a2  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.bonsai.wallet32.WalletApplication.java

public void doExit() {
    mLogger.info("Application exiting");

    if (mWalletService != null)
        mWalletService.shutdown();//from  w w w  .  j ava  2  s.  co  m

    mLogger.info("Stopping WalletService");
    stopService(new Intent(this, WalletService.class));

    // Cancel any remaining notifications.
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancelAll();

    // Kill everything
    mLogger.info("Exiting");
    System.exit(0);
}

From source file:com.brayanarias.alarmproject.activity.AlarmScreenActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_screen);
    int alarmId = getIntent().getIntExtra(Constant.alarmIdKey, -1);
    if (alarmId == -1) {
        finish();//  ww w . j ava2s  .c om
    }
    NotificationManager alarmNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    alarmNotificationManager.cancelAll();
    String name = getIntent().getStringExtra(Constant.alarmNameKey);
    if (name == null || name.isEmpty()) {
        name = getString(R.string.app_name);
    }
    String hourFormatted = getIntent().getStringExtra(Constant.alarmHourFormattedKey);
    String msgampm = getIntent().getStringExtra(Constant.alarmAmPmKey);
    String msgNotification = name;
    msgNotification = msgNotification + " - " + hourFormatted + " " + msgampm;
    sendNotification(msgNotification);
    Calendar calendar = Calendar.getInstance();
    String hourText;
    int actualAmPm = calendar.get(Calendar.AM_PM);
    String ampm = actualAmPm == Calendar.AM ? "AM" : "PM";
    int hour = calendar.get(Calendar.HOUR);
    hour = hour == 0 ? 12 : hour;
    int minute = calendar.get(Calendar.MINUTE);
    String minuteString = minute >= 10 ? "" + minute : "0" + minute;
    hourText = hour + ":" + minuteString + " " + ampm;
    TextView tvAlarmHourScreen = (TextView) findViewById(R.id.tvAlarmHourScreen);
    tvAlarmHourScreen.setText(hourText);
    TextView tvAlarmNameScreen = (TextView) findViewById(R.id.tvAlarmNameScreen);
    if (name.equals(getString(R.string.app_name))) {
        name = AlarmUtilities.getSalute(getApplicationContext());
    }
    if (tvAlarmNameScreen != null) {
        tvAlarmNameScreen.setText(name);
    }
    FloatingActionButton btDismiss = (FloatingActionButton) findViewById(R.id.btDismiss);
    if (btDismiss != null) {
        btDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                if (vibrator != null) {
                    vibrator.cancel();
                }
                finish();
            }
        });
    }
    FloatingActionButton btPostpone = (FloatingActionButton) findViewById(R.id.btPostpone);
    if (btPostpone != null) {
        btPostpone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int alarmId = getIntent().getIntExtra(Constant.alarmIdKey, -1);
                int postpone = getIntent().getIntExtra(Constant.alarmPostponeKey, -1);
                int minutes = getResources().getIntArray(R.array.alarm_postpone_value_array)[postpone];
                AlarmReceiver.dismissAlarm(alarmId, minutes, getApplicationContext());
                String text = AlarmUtilities.getMessageRemainingPostpone(postpone, getResources());
                Toast.makeText(getApplication(), text, Toast.LENGTH_SHORT).show();
                flag = false;
                finish();
            }
        });
    }
    int postpone = getIntent().getIntExtra(Constant.alarmPostponeKey, -1);
    //postpone configuration
    if (postpone > 0) {
        String timeText = getResources().getStringArray(R.array.alarm_postpone_array)[postpone];
    } else {
        if (btPostpone != null) {
            btPostpone.setEnabled(false);
        }
    }
    String tone = getIntent().getStringExtra(Constant.alarmToneKey);
    playRingtone(tone);
    //Start animation
    starAnimation();
    //Ensure wakelock release
    Runnable releaseWakelock = new Runnable() {

        @Override
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

            if (wakeLock != null && wakeLock.isHeld()) {
                wakeLock.release();
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                if (vibrator != null) {
                    vibrator.cancel();
                }
                Log.e(TAG, "Entra a realease wakelock");
                finish();
            }
        }
    };
    new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}

From source file:menion.android.whereyougo.gui.activity.MainActivity.java

@Override
protected void eventDestroyApp() {
    NotificationManager mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancelAll();
}

From source file:com.anhuioss.crowdroid.activity.MoreFunctionActivity.java

private void confirmLogoutDialog() {
    AlertDialog.Builder dlg = new AlertDialog.Builder(this);
    dlg.setTitle(R.string.logout);/* ww  w.  j  a  v  a2  s.  co m*/
    dlg.setMessage(getResources().getString(R.string.wheter_to_logout))
            .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //
                    NotificationManager notificationManager = (NotificationManager) getSystemService(
                            NOTIFICATION_SERVICE);
                    notificationManager.cancelAll();

                    Intent i = new Intent(MoreFunctionActivity.this, LoginActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("autoLogin", false);
                    startActivity(i);
                    //                        android.os.Process
                    //                              .killProcess(android.os.Process.myPid());
                }
            }).setNegativeButton(getResources().getString(R.string.cancel),
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
            .create().show();

}

From source file:com.chen.mail.utils.NotificationUtils.java

/**
 * Get all notifications for all accounts and cancel them.
 **//*www .  jav a2s. c om*/
public static void cancelAllNotifications(Context context) {
    LogUtils.d(LOG_TAG, "cancelAllNotifications - cancelling all");
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancelAll();
    clearAllNotfications(context);
}

From source file:com.clearcenter.mobile_demo.mdAuthenticatorActivity.java

public void onCreate(Bundle bundle) {
    Log.i(TAG, "onCreate(" + bundle + ")");
    super.onCreate(bundle);
    setContentView(R.layout.login_activity);

    account_manager = AccountManager.get(this);

    Log.i(TAG, "loading data from Intent");
    final Intent intent = getIntent();

    nickname = intent.getStringExtra(PARAM_NICKNAME);
    username = intent.getStringExtra(PARAM_USERNAME);
    hostname = intent.getStringExtra(PARAM_HOSTNAME);

    request_new_account = nickname == null;
    confirm_credentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
    Log.i(TAG, "new account? " + request_new_account + ", confirm credentials? " + confirm_credentials);

    scroll_view = (ScrollView) findViewById(R.id.scroll_view);

    message = (TextView) findViewById(R.id.message);

    nickname_label = (TextView) findViewById(R.id.nickname_label);
    nickname_edit = (EditText) findViewById(R.id.nickname_edit);
    nickname_label.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    nickname_edit.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    if (nickname != null)
        nickname_edit.setText(nickname);

    hostname_label = (TextView) findViewById(R.id.hostname_label);
    hostname_edit = (EditText) findViewById(R.id.hostname_edit);
    hostname_label.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    hostname_edit.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    if (hostname != null)
        hostname_edit.setText(hostname);

    username_label = (TextView) findViewById(R.id.username_label);
    username_edit = (EditText) findViewById(R.id.username_edit);
    username_label.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    username_edit.setVisibility(request_new_account ? View.VISIBLE : View.GONE);
    if (username != null)
        username_edit.setText(username);

    password_edit = (EditText) findViewById(R.id.password_edit);

    if (confirm_credentials) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notification_manager;
        notification_manager = (NotificationManager) getApplicationContext().getSystemService(ns);
        notification_manager.cancelAll();
        Log.i(TAG, "TODO: Cancel all notifications?");
    }/* w ww  . ja  v  a 2s . c o m*/

    if (!TextUtils.isEmpty(nickname))
        nickname_edit.setText(nickname);

    if (request_new_account)
        message.setText(getText(R.string.login_activity_new_account));
    else if (confirm_credentials) {
        message.setText(getText(R.string.login_activity_confirm_credentials));
    }

    try {
        mdSSLUtil.DisableSecurity();
    } catch (GeneralSecurityException e) {
        Toast.makeText(getApplicationContext(), e.getMessage(), 4).show();
    }
}