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:net.micode.soundrecorder.RecorderService.java

private void showStoppedNotification() {
    stopForeground(true);/*from  ww  w.  jav a2s .co m*/
    mLowStorageNotification = null;

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.stat_sys_call_record).setContentTitle(getString(R.string.app_name))
            .setWhen(System.currentTimeMillis()).setContentText(getString(R.string.notification_stopped));

    //Support for Android 7 and above  see below
    //https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0

    Intent intent = new Intent(Intent.ACTION_VIEW);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", new File(mFilePath));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("audio/*");
        intent.setDataAndType(uri, "audio/*");
    } else {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("audio/*");
        intent.setDataAndType(Uri.fromFile(new File(mFilePath)), "audio/*");
    }

    PendingIntent pendingIntent;
    pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(pendingIntent);
    Notification noti = mBuilder.build();
    noti.flags = Notification.FLAG_AUTO_CANCEL;
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notifyID, noti);
}

From source file:org.mythdroid.util.UpdateService.java

private void checkMDD(String addr) {

    if (MDDVer == null)
        return;/*from  w w w .j  a  v a2s.c  o m*/

    Version currentVer;
    try {
        currentVer = new Version(MDDManager.getVersion(addr), null);
    } catch (NumberFormatException e) {
        ErrUtil.logErr(e);
        return;
    } catch (IOException e) {
        ErrUtil.logErr(e);
        return;
    }

    if (currentVer.compareTo(MDDVer) >= 0) {
        LogUtil.debug("MDD on " + addr + " is already the latest version" //$NON-NLS-1$ //$NON-NLS-2$
        );
        return;
    }

    LogUtil.debug("MDD ver " + MDDVer.toString() + " is available (current ver on " + //$NON-NLS-1$ //$NON-NLS-2$
            addr + " is " + currentVer.toString() + ")" //$NON-NLS-1$ //$NON-NLS-2$
    );

    if (MDVer != null && MDVer.compareTo(MDDVer) != 0) {
        LogUtil.warn("Version mismatch:" + " MythDroid is " + MDVer.toString() + //$NON-NLS-1$ //$NON-NLS-2$
                ", MDD on " + addr + " is " + MDDVer.toString() //$NON-NLS-1$ //$NON-NLS-2$ 
        );
    }

    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    final Notification notification = new Notification(R.drawable.logo,
            Messages.getString("UpdateService.0") + "MDD" + //$NON-NLS-1$ //$NON-NLS-2$
                    Messages.getString("UpdateService.1"), //$NON-NLS-1$
            System.currentTimeMillis());

    notification.flags = Notification.FLAG_AUTO_CANCEL;

    final Intent intent = new Intent(MDDACTION);
    intent.putExtra(ADDR, addr);
    intent.putExtra(VER, MDDVer.toString());
    intent.putExtra(URL, MDDVer.url.toString());

    notification.setLatestEventInfo(getApplicationContext(), "MDD" + Messages.getString("UpdateService.2"), //$NON-NLS-1$ //$NON-NLS-2$
            MDDVer.toString() + Messages.getString("UpdateService.1") + //$NON-NLS-1$ 
                    Messages.getString("UpdateService.3"), //$NON-NLS-1$
            PendingIntent.getBroadcast(this, 0, intent, 0));

    nm.notify(CHECKMDD, notification);

}

From source file:org.opendatakit.scan.services.ProcessFormsService.java

/**
 * The main function of this method is to pass arguments to the C++ layer and block while it
 * handles the actual processing and computer vision. See the bubblebot_lib tree for that code.
 *
 * But most of the code in this method is just sending notifications about the progress of the
 * processing/*from  w  w w.j  av a 2s.  co m*/
 *
 * @param extras Parameters to be passed to the pending intent that is launched via
 *               notification when the processing is finished
 * @param configJSON Parameters to be passed to the C++ layer
 */
private void processForm(Bundle extras, JSONObject configJSON) {
    final Context context = getApplicationContext();
    final int notificationId = (int) (Math.random() * 9999999);
    final NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    // Send a notification that we have begun processing the form
    Intent waitingIntent = new Intent(context, DisplayStatusActivity.class);
    waitingIntent.putExtras(extras);

    Notification beginNotification = new Notification.Builder(context).setContentTitle(NOTIFICATION_APP_TITLE)
            .setSmallIcon(R.drawable.ic_schedule_white_24dp)
            .setContentText(this.getString(R.string.begin_processing))
            .setContentIntent(PendingIntent.getActivity(context, notificationId, waitingIntent, 0))
            .setWhen(System.currentTimeMillis()).build();
    notificationManager.notify(notificationId, beginNotification);

    // Send the config to the cpp, start processing and block until completion
    Log.i(LOG_TAG, "Using data = " + configJSON);
    final Processor mProcessor = new Processor();//ScanUtils.appFolder
    String jsonString = mProcessor.processViaJSON(configJSON.toString());

    // Check for errors in parsing
    JSONObject resultJSON = null;
    String errorMessage = "";
    try {
        resultJSON = new JSONObject(jsonString);
        errorMessage = resultJSON.optString("errorMessage");
    } catch (JSONException e) {
        Log.i(LOG_TAG, "Unparsable JSON: " + jsonString);
    }

    Notification resultNotification;
    if (errorMessage.length() == 0 && resultJSON != null) {
        // Construct a notification that we have finished processing the form
        Intent finishedIntent = new Intent(context, DisplayProcessedFormActivity.class);
        extras.putString("result", jsonString);
        extras.putString("templatePath", resultJSON.optString("templatePath"));
        finishedIntent.putExtras(extras);

        resultNotification = new Notification.Builder(context).setContentTitle(NOTIFICATION_APP_TITLE)
                .setSmallIcon(R.drawable.ic_done_white_24dp)
                .setContentText(this.getString(R.string.finished_processing))
                .setContentIntent(PendingIntent.getActivity(context, notificationId, finishedIntent, 0))
                .setWhen(System.currentTimeMillis()).build();

    } else {
        // Construct a notification that we had an error processing the form
        Intent errorIntent = new Intent(context, DisplayStatusActivity.class);
        extras.putString("result", jsonString);
        errorIntent.putExtras(extras);

        resultNotification = new Notification.Builder(context).setContentTitle(NOTIFICATION_APP_TITLE)
                .setSmallIcon(R.drawable.ic_error_white_24dp)
                .setContentText(this.getString(R.string.error_processing))
                .setContentIntent(PendingIntent.getActivity(context, notificationId, errorIntent, 0))
                .setWhen(System.currentTimeMillis()).build();
    }

    resultNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationId, resultNotification);
}

From source file:org.sipdroid.sipua.ui.Receiver.java

public static void onText(int type, String text, int mInCallResId, long base) {
    if (mSipdroidEngine != null && type == REGISTER_NOTIFICATION + mSipdroidEngine.pref) {
        cache_text = text;/*  w w w . jav a  2 s . c  o  m*/
        cache_res = mInCallResId;
    }
    if (type >= REGISTER_NOTIFICATION && mInCallResId == R.drawable.sym_presence_available
            && !PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(
                    org.sipdroid.sipua.ui.Settings.PREF_REGISTRATION,
                    org.sipdroid.sipua.ui.Settings.DEFAULT_REGISTRATION))
        text = null;
    NotificationManager mNotificationMgr = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    if (text != null) {
        Notification notification = new Notification();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

        notification.icon = mInCallResId;
        if (type == MISSED_CALL_NOTIFICATION) {
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            //TODO
            /*notification.setLatestEventInfo(mContext, text, mContext.getString(R.string.app_name),
                  PendingIntent.getActivity(mContext, 0, createCallLogIntent(), 0));
            if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_NOTIFY, org.sipdroid.sipua.ui.Settings.DEFAULT_NOTIFY)) {
               notification.flags |= Notification.FLAG_SHOW_LIGHTS;
               notification.ledARGB = 0xff0000ff;  blue 
               notification.ledOnMS = 125;
               notification.ledOffMS = 2875;
            }*/
        } else {
            switch (type) {
            case MWI_NOTIFICATION:
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.contentIntent = PendingIntent.getActivity(mContext, 0, createMWIIntent(), 0);
                notification.flags |= Notification.FLAG_SHOW_LIGHTS;
                notification.ledARGB = 0xff00ff00; /* green */
                notification.ledOnMS = 125;
                notification.ledOffMS = 2875;
                break;
            case AUTO_ANSWER_NOTIFICATION:
                notification.contentIntent = PendingIntent.getActivity(mContext, 0,
                        createIntent(AutoAnswer.class), 0);
                break;
            default:
                if (type >= REGISTER_NOTIFICATION && mSipdroidEngine != null
                        && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref
                        && mInCallResId == R.drawable.sym_presence_available) {
                    int key = type - REGISTER_NOTIFICATION;
                    Intent in = createIntent(ChangeAccount.class);
                    in.setAction(Long.toString(type));
                    in.putExtra(ChangeAccount.ChangeAccountKey, key);
                    in.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    notification.contentIntent = PendingIntent.getActivity(mContext, key, in,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                } else
                    notification.contentIntent = PendingIntent.getActivity(mContext, 0,
                            createIntent(Sipdroid.class), 0);
                if (mInCallResId == R.drawable.sym_presence_away) {
                    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
                    notification.ledARGB = 0xffff0000; /* red */
                    notification.ledOnMS = 125;
                    notification.ledOffMS = 2875;
                }
                notification.flags |= Notification.FLAG_ONGOING_EVENT;
                break;
            }

            RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
                    R.layout.ongoing_call_notification);
            contentView.setImageViewResource(R.id.icon, notification.icon);
            if (base != 0) {
                contentView.setChronometer(R.id.text1, base, text + " (%s)", true);
            } else if (type >= REGISTER_NOTIFICATION) {
                if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(
                        org.sipdroid.sipua.ui.Settings.PREF_POS, org.sipdroid.sipua.ui.Settings.DEFAULT_POS))
                    contentView.setTextViewText(R.id.text2,
                            text + "/" + mContext.getString(R.string.settings_pos3));
                else
                    contentView.setTextViewText(R.id.text2, text);
                if (mSipdroidEngine != null)
                    contentView.setTextViewText(R.id.text1,
                            mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].username + "@"
                                    + mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].realm_orig);
            } else
                contentView.setTextViewText(R.id.text1, text);
            notification.contentView = contentView;
        }
        mNotificationMgr.notify(type, notification);
    } else {
        mNotificationMgr.cancel(type);
    }
    if (type != AUTO_ANSWER_NOTIFICATION)
        updateAutoAnswer();
    if (mSipdroidEngine != null && type >= REGISTER_NOTIFICATION
            && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref)
        onText(REGISTER_NOTIFICATION + mSipdroidEngine.pref, cache_text, cache_res, 0);
}

From source file:com.siahmsoft.soundroid.sdk7.services.UploadService.java

/**
 * Show a notification while this service is running.
 *//*from  w w  w  .  j a v a  2  s . c  o  m*/
private void showNotification(String text) {
    // Set the icon, scrolling text and timestamp
    Notification notification = new Notification(R.drawable.uploadtocloud, text, System.currentTimeMillis());

    // The PendingIntent to launch our activity if the user selects this notification
    Intent i = new Intent(this, MeActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    /*Bundle bundle = new Bundle();
    bundle.putInt("idTrack", soundcloudTrack.getmIdTrack());
    i.putExtras(bundle);*/

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this, "Uploading...", text, contentIntent);

    // We show this for as long as our service is processing a command.
    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;
    notification.number = totalUploads;

    // Send the notification.
    // We use a string id because it is a unique number.  We use it later to cancel.
    mNM.notify(777, notification);
}

From source file:com.freeme.filemanager.FileExplorerTabActivity.java

public void showButtonNotify() {
    mBuilder = new Builder(this);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);
    if (IsFreemeOs.isFreemeOs()) {
        Bitmap bitMap = getApplicationContext().getPackageManager().getIconBitmapWithThemeBg(
                getResources().getDrawable(R.drawable.notification_clean),
                getApplicationContext().getPackageName(), 0);
        remoteViews.setImageViewBitmap(R.id.filemanager_notification_clean, bitMap);
    } else {//from   w w w  .ja  v  a2  s.c o  m
        remoteViews.setImageViewResource(R.id.filemanager_notification_clean, R.drawable.notification_clean);
    }
    remoteViews.setTextViewText(R.id.tv_custom_title, getString(R.string.fileManager));
    remoteViews.setTextViewText(R.id.tv_custom_text, getString(R.string.clean_text));

    Intent buttonIntent = new Intent(ACTION_BUTTON);
    buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_CLEAN_ID);
    PendingIntent intent_clean = PendingIntent.getBroadcast(this, 1, buttonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.btn_custom, intent_clean);

    mBuilder.setContent(remoteViews).setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
            .setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_DEFAULT).setOngoing(true)
            .setSmallIcon(R.drawable.notification_small_clean);
    notify = mBuilder.build();
    notify.flags = Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(200, notify);
}

From source file:com.fanfou.app.opensource.service.DownloadService.java

private void showProgress() {
    this.notification = new Notification(R.drawable.ic_notify_download, "?",
            System.currentTimeMillis());
    this.notification.flags |= Notification.FLAG_ONGOING_EVENT;
    this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
    this.notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    final RemoteViews view = new RemoteViews(getPackageName(), R.layout.download_notification);
    view.setTextViewText(R.id.download_notification_text, "? 0%");
    view.setProgressBar(R.id.download_notification_progress, 100, 0, false);
    this.notification.contentView = view;
    this.nm.notify(DownloadService.NOTIFICATION_PROGRESS_ID, this.notification);
}

From source file:com.quarterfull.newsAndroid.ssl.MemorizingTrustManager.java

void startActivityNotification(Intent intent, String certName) {
    PendingIntent call = PendingIntent.getActivity(mContext, 0, intent, 0);
    Notification n = new NotificationCompat.Builder(mContext)
            .setLargeIcon(/*ww w. ja  va2s .  c o m*/
                    BitmapFactory.decodeResource(mContext.getResources(), android.R.drawable.ic_lock_lock))
            .setSmallIcon(android.R.drawable.ic_lock_lock)
            .setContentTitle(mContext.getString(R.string.app_name))
            .setContentText(mContext.getString(R.string.mtm_notification))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(certName)).setContentIntent(call).build();

    n.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(NOTIFICATION_ID, n);
}

From source file:com.airbop.library.simple.AirBopGCMIntentService.java

private static void generateImageNotification(Context context, String title, String message, String url,
        String image_url, String large_icon) {

    // The bitmap to download
    Bitmap message_bitmap = null;/*from w  w  w .  ja  v a 2 s  .  c o m*/
    // Should we download the image?
    if ((image_url != null) && (!image_url.equals(""))) {
        message_bitmap = AirBopImageDownloader.downloadBitmap(image_url, context);
    }
    // If we didn't get the image, we're out of here
    if (message_bitmap == null) {
        generateNotification(context, title, message, url, large_icon);
        return;
    }
    AirBopManifestSettings airBop_settings = CommonUtilities.loadDataFromManifest(context);

    int icon = airBop_settings.mNotificationIcon;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //if ((title == null) || (title.equals(""))) {
    if (title == null) {
        title = airBop_settings.mDefaultNotificationTitle;
    }

    Intent notificationIntent = null;
    if ((url == null) || (url.equals(""))) {
        //just bring up the app
        if (context != null) {
            ClassLoader class_loader = context.getClassLoader();
            if (class_loader != null) {
                try {
                    if (airBop_settings.mDefaultNotificationClass != null) {
                        notificationIntent = new Intent(context,
                                Class.forName(airBop_settings.mDefaultNotificationClass));
                    }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } else {
        //Launch the URL
        notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse(url));
        notificationIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    }
    PendingIntent intent = null;
    // set intent so it does not start a new activity
    if (notificationIntent != null) {
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    }

    Builder notificationBuilder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setLargeIcon(decodeImage(large_icon)).setWhen(when)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(message_bitmap));
    if (intent != null) {
        notificationBuilder.setContentIntent(intent);
    }
    if (icon != 0) {
        notificationBuilder.setSmallIcon(icon);
    }
    Notification notification = notificationBuilder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}