Example usage for android.app Notification FLAG_AUTO_CANCEL

List of usage examples for android.app Notification FLAG_AUTO_CANCEL

Introduction

In this page you can find the example usage for android.app Notification FLAG_AUTO_CANCEL.

Prototype

int FLAG_AUTO_CANCEL

To view the source code for android.app Notification FLAG_AUTO_CANCEL.

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if the notification should be canceled when it is clicked by the user.

Usage

From source file:com.manuelmazzuola.speedtogglebluetooth.service.MonitorSpeed.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    running = Boolean.TRUE;/*from  www. ja v  a  2  s  .c  o  m*/

    lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    provider = lm.NETWORK_PROVIDER;
    oldLocation = lm.getLastKnownLocation(provider);

    IntentFilter filters = new IntentFilter();
    // When to turn off bluetooth
    filters.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    // When hold bluetooth on
    filters.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    // When user directly turn on or off bluetooth
    filters.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

    registerReceiver(bluetoothListener, filters);
    lm.requestLocationUpdates(provider, 45 * 1000, 0f, this);

    Intent stopIntent = new Intent(this, MainActivity.class);
    stopIntent.putExtra("close", "close");
    PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0, stopIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder note = new NotificationCompat.Builder(getApplicationContext())
            .setContentTitle(DEFAULT_TITLE).setContentText(DEFAULT_MESSAGE)
            .setDefaults(Notification.DEFAULT_VIBRATE).setAutoCancel(true).setContentIntent(stopPendingIntent)
            .setSmallIcon(R.drawable.ic_action_bluetooth);

    note.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;

    Notification notification = note.build();
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

    startForeground(intentId, note.build());

    return START_NOT_STICKY;
}

From source file:com.afrolkin.samplepushclient.GCMIntentService.java

private static void generateNotification(Context context, String message) {
    int icon = R.mipmap.ic_announcement_black_48dp;
    long when = System.currentTimeMillis();
    String title = context.getString(R.string.app_name);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, "Push Message", when);
    notification.sound = soundUri;//from   w  w  w  .  j  a  va2  s.  c o m

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("message", message);
    // Set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:com.ushahidi.android.app.BackgroundService.java

private void showNotification(String tickerText) {

    // This is what should be launched if the user selects our notification.
    Intent baseIntent = new Intent(this, IncidentTab.class);
    baseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, baseIntent, 0);

    // choose the ticker text
    newUshahidiReportNotification = new Notification(R.drawable.notification_icon, tickerText,
            System.currentTimeMillis());
    newUshahidiReportNotification.contentIntent = contentIntent;
    newUshahidiReportNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newUshahidiReportNotification.defaults = Notification.DEFAULT_ALL;
    newUshahidiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent);

    if (Preferences.ringtone) {
        // set the ringer
        Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3"));
        newUshahidiReportNotification.sound = ringURI;
    }/*from w ww  . ja  v a  2s . c o m*/

    if (Preferences.vibrate) {
        double vibrateLength = 100 * Math.exp(0.53 * 20);
        long[] vibrate = new long[] { 100, 100, (long) vibrateLength };
        newUshahidiReportNotification.vibrate = vibrate;

        if (Preferences.flashLed) {
            int color = Color.BLUE;
            newUshahidiReportNotification.ledARGB = color;
        }

        newUshahidiReportNotification.ledOffMS = (int) vibrateLength;
        newUshahidiReportNotification.ledOnMS = (int) vibrateLength;
        newUshahidiReportNotification.flags = newUshahidiReportNotification.flags
                | Notification.FLAG_SHOW_LIGHTS;
    }

    mNotificationManager.notify(Preferences.NOTIFICATION_ID, newUshahidiReportNotification);
}

From source file:com.ushahidi.android.app.UshahidiService.java

private void showNotification(String tickerText) {

    // This is what should be launched if the user selects our notification.
    Intent baseIntent = new Intent(this, IncidentsTab.class);
    baseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, baseIntent, 0);

    // choose the ticker text
    newUshahidiReportNotification = new Notification(R.drawable.favicon, tickerText,
            System.currentTimeMillis());
    newUshahidiReportNotification.contentIntent = contentIntent;
    newUshahidiReportNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newUshahidiReportNotification.defaults = Notification.DEFAULT_ALL;
    newUshahidiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent);

    if (UshahidiPref.ringtone) {
        // set the ringer
        Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3"));
        newUshahidiReportNotification.sound = ringURI;
    }/*from w ww.jav  a 2  s . co m*/

    if (UshahidiPref.vibrate) {
        double vibrateLength = 100 * Math.exp(0.53 * 20);
        long[] vibrate = new long[] { 100, 100, (long) vibrateLength };
        newUshahidiReportNotification.vibrate = vibrate;

        if (UshahidiPref.flashLed) {
            int color = Color.BLUE;
            newUshahidiReportNotification.ledARGB = color;
        }

        newUshahidiReportNotification.ledOffMS = (int) vibrateLength;
        newUshahidiReportNotification.ledOnMS = (int) vibrateLength;
        newUshahidiReportNotification.flags = newUshahidiReportNotification.flags
                | Notification.FLAG_SHOW_LIGHTS;
    }

    mNotificationManager.notify(UshahidiPref.NOTIFICATION_ID, newUshahidiReportNotification);
}

From source file:net.bible.android.device.ProgressNotificationManager.java

/** find the Progress object in our map to the associated Notifications
 * //w w  w.  j a  v a2 s .  c  o  m
 * @param prog
 * @return
 */
private Notification findOrCreateNotification(Progress prog) {
    Notification notification = progressMap.get(prog);
    if (notification == null) {
        Log.d(TAG, "Creating Notification for progress Hash:" + prog.hashCode());
        // configure the intent
        Intent intent = new Intent(BibleApplication.getApplication(), ProgressStatus.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(BibleApplication.getApplication(), 0,
                intent, 0);

        notification = new Notification(R.drawable.bible, prog.getJobName(), System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_AUTO_CANCEL;
        notification.contentView = new RemoteViews(SharedConstants.PACKAGE_NAME,
                R.layout.progress_notification);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.bible);
        notification.contentView.setTextViewText(R.id.status_text, "");
        notification.contentView.setProgressBar(R.id.status_progress, 100, prog.getWork(), false);

        progressMap.put(prog, notification);

        androidNotificationManager.notify(prog.hashCode(), notification);
    }

    return notification;
}

From source file:net.bible.service.device.ProgressNotificationManager.java

/** find the Progress object in our map to the associated Notifications
 * /*from  w  ww  .j  a v  a2s  .c  o m*/
 * @param prog
 * @return
 */
private Notification findOrCreateNotification(Progress prog) {
    Notification notification = progressMap.get(prog);
    if (notification == null) {
        Log.d(TAG, "Creating Notification for progress Hash:" + prog.hashCode());
        // configure the intent
        Intent intent = new Intent(BibleApplication.getApplication(), ProgressStatus.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(BibleApplication.getApplication(), 0,
                intent, 0);

        notification = new Notification(R.drawable.ic_stat_ichthys, prog.getJobName(),
                System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_AUTO_CANCEL;
        notification.contentView = new RemoteViews(SharedConstants.PACKAGE_NAME,
                R.layout.progress_notification);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ichthys);
        notification.contentView.setTextViewText(R.id.status_text, "");
        notification.contentView.setProgressBar(R.id.status_progress, 100, prog.getWork(), false);

        progressMap.put(prog, notification);

        androidNotificationManager.notify(prog.hashCode(), notification);
    }

    return notification;
}

From source file:com.cyeam.cInterphone.ui.CInterphone.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // Set default fragment.
    mViewPager.setCurrentItem(1);/*from   w w  w.j  av a  2 s . c o m*/

    // Account[] accounts = AccountManager.get(this).getAccountsByType(
    // "com.google");
    // if (accounts.length > 0) {
    // Settings.DEFAULT_USERNAME = accounts[0].name;
    // }
    Receiver.engine(this).StartEngine();

    PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
            Utils.getMetaValue(CInterphone.this, "api_key"));
    CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(this,
            R.layout.ongoing_call_notification, R.id.icon, R.id.text1, R.id.text2);
    cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
    cBuilder.setNotificationDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    cBuilder.setStatusbarIcon(R.drawable.icon64);
    cBuilder.setLayoutDrawable(R.drawable.icon22);
    PushManager.setNotificationBuilder(this, 0, cBuilder);

    Push.Push(new com.cyeam.cInterphone.model.Notification("hello, world"));
}

From source file:io.lqd.sdk.LQPushHandler.java

@SuppressWarnings("deprecation")
private void sendNotification(Context c, PendingIntent intent, int icon, int push_id, String title, String body,
        Uri sound) {/* w  w w .j  a  v a  2  s  . c o  m*/
    NotificationManager nm = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(c).setSmallIcon(icon).setTicker(body)
            .setContentText(body).setWhen(System.currentTimeMillis()).setContentTitle(title)
            .setContentIntent(intent);
    if (sound != null)
        builder.setSound(sound);
    Notification n = builder.build();
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(push_id, n);
}

From source file:com.akop.bach.service.XboxLiveServiceClient.java

private void notifyMessages(XboxLiveAccount account, long[] unreadMessages, List<Long> lastUnreadList) {
    NotificationManager mgr = getNotificationManager();
    Context context = getContext();

    int notificationId = 0x1000000 | ((int) account.getId() & 0xffffff);

    if (App.getConfig().logToConsole()) {
        String s = "";
        for (Object unr : lastUnreadList)
            s += unr.toString() + ",";

        App.logv("Currently unread (%d): %s", lastUnreadList.size(), s);

        s = "";/*w w w .j ava 2 s .c o m*/
        for (Object unr : unreadMessages)
            s += unr.toString() + ",";

        App.logv("New unread (%d): %s", unreadMessages.length, s);
    }

    if (unreadMessages.length > 0) {
        int unreadCount = 0;
        for (Object unread : unreadMessages) {
            if (!lastUnreadList.contains(unread))
                unreadCount++;
        }

        if (App.getConfig().logToConsole())
            App.logv("%d computed new", unreadCount);

        if (unreadCount > 0) {
            String tickerTitle;
            String tickerText;

            if (unreadMessages.length == 1) {
                tickerTitle = context.getString(R.string.new_message);
                tickerText = context.getString(R.string.notify_message_pending_f, account.getScreenName(),
                        Messages.getSender(context, unreadMessages[0]));
            } else {
                tickerTitle = context.getString(R.string.new_messages);
                tickerText = context.getString(R.string.notify_messages_pending_f, account.getScreenName(),
                        unreadMessages.length, account.getDescription());
            }

            Notification notification = new Notification(R.drawable.xbox_stat_notify_message, tickerText,
                    System.currentTimeMillis());

            Intent intent = new Intent(context, MessageList.class);
            intent.putExtra("account", account);

            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

            notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

            if (unreadMessages.length > 1)
                notification.number = unreadMessages.length;

            notification.ledOnMS = DEFAULT_LIGHTS_ON_MS;
            notification.ledOffMS = DEFAULT_LIGHTS_OFF_MS;
            notification.ledARGB = DEFAULT_LIGHTS_COLOR;
            notification.sound = account.getRingtoneUri();

            if (account.isVibrationEnabled())
                notification.defaults |= Notification.DEFAULT_VIBRATE;

            notification.setLatestEventInfo(context, tickerTitle, tickerText, contentIntent);

            mgr.notify(notificationId, notification);
        }
    } else // No unread messages
    {
        mgr.cancel(notificationId);
    }
}

From source file:com.bellman.bible.service.device.ProgressNotificationManager.java

/**
 * find the Progress object in our map to the associated Notifications
 *
 * @param prog/*  w ww  .  j  av  a  2 s .c o m*/
 * @return
 */
private Notification findOrCreateNotification(Progress prog) {
    Notification notification = progressMap.get(prog);
    if (notification == null) {
        Log.d(TAG, "Creating Notification for progress Hash:" + prog.hashCode());
        // configure the intent
        Intent intent = new Intent(CurrentActivityHolder.getInstance().getApplication(), ProgressStatus.class);
        final PendingIntent pendingIntent = PendingIntent
                .getActivity(CurrentActivityHolder.getInstance().getApplication(), 0, intent, 0);

        notification = new Notification(R.drawable.ic_stat_ichthys, prog.getJobName(),
                System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_AUTO_CANCEL;
        notification.contentView = new RemoteViews(SharedConstants.PACKAGE_NAME,
                R.layout.progress_notification);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ichthys);
        notification.contentView.setTextViewText(R.id.status_text, "");
        notification.contentView.setProgressBar(R.id.status_progress, 100, prog.getWork(), false);

        progressMap.put(prog, notification);

        androidNotificationManager.notify(prog.hashCode(), notification);
    }

    return notification;
}