Example usage for android.widget RemoteViews RemoteViews

List of usage examples for android.widget RemoteViews RemoteViews

Introduction

In this page you can find the example usage for android.widget RemoteViews RemoteViews.

Prototype

public RemoteViews(RemoteViews landscape, RemoteViews portrait) 

Source Link

Document

Create a new RemoteViews object that will inflate as the specified landspace or portrait RemoteViews, depending on the current configuration.

Usage

From source file:com.granita.tasks.notification.NotificationActionUtils.java

/**
 * Creates and displays an Undo notification for the specified {@link NotificationAction}.
 *///from   ww  w. j a  v a 2  s.co  m
public static void createUndoNotification(final Context context, NotificationAction action) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(context.getString(action.getActionTextResId()

    ));
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setWhen(action.getWhen());

    // disable sound & vibration
    builder.setDefaults(0);

    final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification);
    undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId));

    final String packageName = context.getPackageName();

    final Intent clickIntent = new Intent(ACTION_UNDO);
    clickIntent.setPackage(packageName);
    putNotificationActionExtra(clickIntent, action);
    final PendingIntent clickPendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent);
    builder.setContent(undoView);

    // When the notification is cleared, we perform the destructive action
    final Intent deleteIntent = new Intent(ACTION_DESTRUCT);
    deleteIntent.setPackage(packageName);
    putNotificationActionExtra(deleteIntent, action);
    final PendingIntent deletePendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setDeleteIntent(deletePendingIntent);

    final Notification notification = builder.build();

    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(action.getNotificationId(), notification);

    sUndoNotifications.put(action.getNotificationId(), action);
    sNotificationTimestamps.put(action.getNotificationId(), action.mWhen);
}

From source file:ru.org.sevn.audiobookplayer.AppService.java

private NotificationCompat.Builder makeNotify(Context ctx, String title, String text) {
    //Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
    NotificationCompat.Builder mBuilder = makeNotifyBuilder(ctx, title, text);

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
    notificationView.setTextViewText(R.id.tvTitle, title);
    notificationView.setTextViewText(R.id.textView1, "" + makeNotifyNumber() + " " + text);
    notificationView.setImageViewBitmap(R.id.imageView1, getLargeNotificationIcon());
    mBuilder.setContent(notificationView);
    mBuilder.setContentIntent(makePendingNotificationIntent(makeNotificationIntent()));

    //       notification.contentView = notificationView;
    //       notification.contentIntent = makePendingNotificationIntent(makeNotificationIntent());
    //       notification.flags |= Notification.FLAG_NO_CLEAR;

    notificationView.setOnClickPendingIntent(R.id.btn_prev, makeActionPendingIntent(ACTION_PREV));
    notificationView.setOnClickPendingIntent(R.id.btn_play, makeActionPendingIntent(ACTION_PLAY_PAUSE));
    notificationView.setOnClickPendingIntent(R.id.btn_next, makeActionPendingIntent(ACTION_NEXT));
    notificationView.setOnClickPendingIntent(R.id.btn_exit, makeActionPendingIntent(ACTION_EXIT));

    return mBuilder;
}

From source file:com.android.deskclock.data.TimerNotificationBuilderN.java

@Override
public Notification buildHeadsUp(Context context, List<Timer> expired) {
    final Timer timer = expired.get(0);

    // First action intent is to reset all timers.
    final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_stop_24dp);
    final Intent reset = TimerService.createResetExpiredTimersIntent(context);
    final PendingIntent intent1 = Utils.pendingServiceIntent(context, reset);

    // Generate some descriptive text, a title, and an action name based on the timer count.
    final CharSequence stateText;
    final int count = expired.size();
    final List<Notification.Action> actions = new ArrayList<>(2);
    if (count == 1) {
        final String label = timer.getLabel();
        if (TextUtils.isEmpty(label)) {
            stateText = context.getString(R.string.timer_times_up);
        } else {//from   ww  w . j a  v  a  2 s.  com
            stateText = label;
        }

        // Left button: Reset single timer
        final CharSequence title1 = context.getString(R.string.timer_stop);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Add minute
        final Intent addTime = TimerService.createAddMinuteTimerIntent(context, timer.getId());
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, addTime);
        final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_add_24dp);
        final CharSequence title2 = context.getString(R.string.timer_plus_1_min);
        actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());

    } else {
        stateText = context.getString(R.string.timer_multi_times_up, count);

        // Left button: Reset all timers
        final CharSequence title1 = context.getString(R.string.timer_stop_all);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());
    }

    final long base = getChronometerBase(timer);

    final String pname = context.getPackageName();
    final RemoteViews contentView = new RemoteViews(pname, R.layout.chronometer_notif_content);
    contentView.setChronometerCountDown(R.id.chronometer, true);
    contentView.setChronometer(R.id.chronometer, base, null, true);
    contentView.setTextViewText(R.id.state, stateText);

    // Content intent shows the timer full screen when clicked.
    final Intent content = new Intent(context, ExpiredTimersActivity.class);
    final PendingIntent contentIntent = Utils.pendingActivityIntent(context, content);

    // Full screen intent has flags so it is different than the content intent.
    final Intent fullScreen = new Intent(context, ExpiredTimersActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    final PendingIntent pendingFullScreen = Utils.pendingActivityIntent(context, fullScreen);

    return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false)
            .setAutoCancel(false).setContentIntent(contentIntent).setCustomContentView(contentView)
            .setPriority(Notification.PRIORITY_MAX).setDefaults(Notification.DEFAULT_LIGHTS)
            .setColor(ContextCompat.getColor(context, R.color.default_background))
            .setSmallIcon(R.drawable.stat_notify_timer).setFullScreenIntent(pendingFullScreen, true)
            .setStyle(new Notification.DecoratedCustomViewStyle())
            .setActions(actions.toArray(new Notification.Action[actions.size()])).build();
}

From source file:net.ccghe.emocha.services.ServerService.java

private void showNotification(String tickertxt, String displayTxt, int max, int progress) {
    NotificationManager notifMgr = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainMenu.class), 0);

    // construct the Notification object.
    Notification notif = new Notification();

    notif.tickerText = tickertxt;/*from w  w w  .j av a2 s.  com*/
    notif.icon = R.drawable.icon;

    nmView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
    nmView.setProgressBar(R.id.progressbar, max, progress, false);
    nmView.setTextViewText(R.id.TextView01, displayTxt);

    notif.contentView = nmView;

    notif.contentIntent = contentIntent;
    notifMgr.notify(R.layout.custom_notification_layout, notif);
}

From source file:com.gelakinetic.mtgfam.helpers.DbUpdaterService.java

protected void switchToUpdating(String title) {
    final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.progress_notification);
    contentView.setProgressBar(R.id.progress_notification_bar, 100, 0, false);
    contentView.setTextViewText(R.id.progress_notification_title, title);

    mUpdateNotification.contentView = contentView;
    mUpdateNotification.contentIntent = mNotificationIntent;

    mNotificationManager.notify(STATUS_NOTIFICATION, mUpdateNotification);

    mProgressUpdater = new Runnable() {
        public void run() {
            contentView.setProgressBar(R.id.progress_notification_bar, 100, mProgress, false);
            mNotificationManager.notify(STATUS_NOTIFICATION, mUpdateNotification);
            mHandler.postDelayed(mProgressUpdater, 200);
        }//from w w  w .  j a  v a 2  s  . c  om
    };
    mHandler.postDelayed(mProgressUpdater, 200);
}

From source file:com.devbrackets.android.exomedia.EMNotification.java

/**
 * Creates the RemoteViews used for the custom (standard) notification
 *
 * @return The resulting RemoteViews/*w w w  .java 2 s .  c o m*/
 */
private RemoteViews getCustomNotification() {
    if (customNotification == null) {
        customNotification = new RemoteViews(context.getPackageName(), R.layout.exomedia_notification_content);

        customNotification.setOnClickPendingIntent(R.id.exomedia_notification_playpause,
                createPendingIntent(EMRemoteActions.ACTION_PLAY_PAUSE, mediaServiceClass));
        customNotification.setOnClickPendingIntent(R.id.exomedia_notification_next,
                createPendingIntent(EMRemoteActions.ACTION_NEXT, mediaServiceClass));
        customNotification.setOnClickPendingIntent(R.id.exomedia_notification_prev,
                createPendingIntent(EMRemoteActions.ACTION_PREVIOUS, mediaServiceClass));
    }

    customNotification.setTextViewText(R.id.exomedia_notification_title, notificationInfo.getTitle());
    customNotification.setTextViewText(R.id.exomedia_notification_album, notificationInfo.getAlbum());
    customNotification.setTextViewText(R.id.exomedia_notification_artist, notificationInfo.getArtist());
    if (notificationInfo.getLargeImage() != null) {
        customNotification.setBitmap(R.id.exomedia_notification_large_image, "setImageBitmap",
                notificationInfo.getLargeImage());
    }

    if (notificationInfo.getMediaState() != null) {
        updateCustomNotificationMediaState(customNotification);
    }

    return customNotification;
}

From source file:com.gaze.webpaser.StackWidgetService.java

public RemoteViews getViewAt(int position) {

    Log.i(LOG_TAG, "getViewAt " + position);
    // position will always range from 0 to getCount() - 1.

    // We construct a remote views item based on our widget item xml file,
    // and set the
    // text based on the position.
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.item_widget);
    rv.setTextViewText(R.id.textView_title, mlist.get(position).getTitle());
    String time = Util.getFormatTime(mlist.get(position).getTime());
    rv.setTextViewText(R.id.textView3_time, time);

    // Next, we set a fill-intent which will be used to fill-in the pending
    // intent template
    // which is set on the collection view in StackWidgetProvider.
    Bundle extras = new Bundle();
    extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);// w  w  w  . j a  v  a 2 s  .c  o m
    rv.setOnClickFillInIntent(R.id.widget1, fillInIntent);

    // You can do heaving lifting in here, synchronously. For example, if
    // you need to
    // process an image, fetch something from the network, etc., it is ok to
    // do it here,
    // synchronously. A loading view will show up in lieu of the actual
    // contents in the
    // interim.
    // try {
    // System.out.println("Loading view " + position);
    // Thread.sleep(500);
    // } catch (InterruptedException e) {
    // e.printStackTrace();
    // }
    Bitmap bitmap = getBitmap(GlobalData.baseUrl + mlist.get(position).getImagePath());
    if (bitmap != null)
        rv.setImageViewBitmap(R.id.imageView1, bitmap);

    // Return the remote views object.
    return rv;
}

From source file:com.actionbarsherlock.sample.hcgallery.MainActivity.java

void showNotification(boolean custom) {
    final Resources res = getResources();
    final NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);/*w ww.jav a  2s . c o  m*/

    Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notify_example).setAutoCancel(true)
            .setTicker(getString(R.string.notification_text))
            .setContentIntent(getDialogPendingIntent("Tapped the notification entry."));

    if (custom) {
        // Sets a custom content view for the notification, including an image button.
        RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
        layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
        layout.setOnClickPendingIntent(R.id.notification_button,
                getDialogPendingIntent("Tapped the 'dialog' button in the notification."));
        builder.setContent(layout);

        // Notifications in Android 3.0 now have a standard mechanism for displaying large
        // bitmaps such as contact avatars. Here, we load an example image and resize it to the
        // appropriate size for large bitmaps in notifications.
        Bitmap largeIconTemp = BitmapFactory.decodeResource(res, R.drawable.notification_default_largeicon);
        Bitmap largeIcon = Bitmap.createScaledBitmap(largeIconTemp,
                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height), false);
        largeIconTemp.recycle();

        builder.setLargeIcon(largeIcon);

    } else {
        builder.setNumber(7) // An example number.
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.notification_text));
    }

    notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
}

From source file:no.firestorm.weathernotificatonservice.WeatherNotificationService.java

/**
 * Make notification and post it to the NotificationManager
 * /* w  w  w  .  ja v  a2  s  .c o  m*/
 * @param tickerIcon
 *            Icon shown in notification bar
 * @param contentIcon
 *            Icon shown in notification
 * @param tickerText
 *            Text shown in notification bar
 * @param contentTitle
 *            Title shown in notification
 * @param contentText
 *            Description shown in notification
 * @param contentTime
 *            Time shown in notification
 * @param when2
 */
private void makeNotification(int tickerIcon, int contentIcon, CharSequence tickerText,
        CharSequence contentTitle, CharSequence contentText, CharSequence contentTime, long when2,
        Float temperature) {
    final long when = System.currentTimeMillis();
    // Make notification
    Notification notification = null;

    final Intent notificationIntent = new Intent(WeatherNotificationService.this,
            WeatherNotificationService.class);
    final PendingIntent contentIntent = PendingIntent.getService(WeatherNotificationService.this, 0,
            notificationIntent, 0);

    // Check if Notification.Builder exists (11+)
    if (Build.VERSION.SDK_INT >= 11) {
        // Honeycomb ++
        NotificationBuilder builder = new NotificationBuilder(this);
        builder.setAutoCancel(false);
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setTicker(tickerText);
        builder.setWhen(when2);
        builder.setSmallIcon(tickerIcon);
        builder.setOngoing(true);
        builder.setContentIntent(contentIntent);
        if (temperature != null)
            builder.makeContentView(contentTitle, contentText, when2, temperature, tickerIcon);
        notification = builder.getNotification();
    } else {
        // Gingerbread --
        notification = new Notification(tickerIcon, tickerText, when);
        notification.flags = Notification.FLAG_ONGOING_EVENT;

        final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.weathernotification);
        contentView.setImageViewResource(R.id.icon, contentIcon);
        contentView.setTextViewText(R.id.title, contentTitle);
        contentView.setTextViewText(R.id.title, contentTitle);
        contentView.setTextViewText(R.id.text, contentText);
        contentView.setTextViewText(R.id.time, contentTime);
        notification.contentView = contentView;
    }
    notification.contentIntent = contentIntent;

    // Post notification
    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, notification);
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

/**
 * Creates and displays an Undo notification for the specified {@link NotificationAction}.
 *///from w  w w. ja va  2 s .  c  o  m
public static void createUndoNotification(final Context context, NotificationAction action) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(context.getString(action.getActionTextResId()

    ));
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setWhen(action.getWhen());

    // disable sound & vibration
    builder.setDefaults(0);

    final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification);
    undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId));

    final String packageName = context.getPackageName();

    final Intent clickIntent = new Intent(context, NotificationUpdaterService.class);
    clickIntent.setAction(ACTION_UNDO);
    clickIntent.setPackage(packageName);
    putNotificationActionExtra(clickIntent, action);
    final PendingIntent clickPendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent);
    builder.setContent(undoView);

    // When the notification is cleared, we perform the destructive action
    final Intent deleteIntent = new Intent(context, NotificationUpdaterService.class);
    deleteIntent.setAction(ACTION_DESTRUCT);
    deleteIntent.setPackage(packageName);
    putNotificationActionExtra(deleteIntent, action);
    final PendingIntent deletePendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setDeleteIntent(deletePendingIntent);

    final Notification notification = builder.build();

    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(action.getNotificationId(), notification);

    sUndoNotifications.put(action.getNotificationId(), action);
    sNotificationTimestamps.put(action.getNotificationId(), action.mWhen);
}