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.gecq.musicwave.player.NotificationHelper.java

/**
 * Call this to build the {@link Notification}.
 *///from w  w w  .  j  a  va2  s  . c om
@SuppressLint("NewApi")
public void buildNotification(final String albumName, final String artistName, final String trackName,
        final Long albumId, final Bitmap albumArt, final boolean isPlaying) {

    // Default notfication layout
    mNotificationTemplate = new RemoteViews(mService.getPackageName(), R.layout.notification_template_base);

    // Set up the content view
    initCollapsedLayout(trackName, artistName, albumArt);

    // Notification Builder
    mNotification = new NotificationCompat.Builder(mService).setSmallIcon(R.drawable.notification_icon)
            .setContentIntent(getPendingIntent()).setPriority(Notification.PRIORITY_DEFAULT)
            .setContent(mNotificationTemplate).build();
    // Control playback from the notification
    initPlaybackActions(isPlaying);
    if (CommonUtils.hasJellyBean()) {
        // Expanded notifiction style
        mExpandedView = new RemoteViews(mService.getPackageName(),
                R.layout.notification_template_expanded_base);
        mNotification.bigContentView = mExpandedView;
        // Control playback from the notification
        initExpandedPlaybackActions(isPlaying);
        // Set up the expanded content view
        initExpandedLayout(trackName, albumName, artistName, albumArt);
    }
    mService.startForeground(MUSIC_WAVE_SERVICE, mNotification);
}

From source file:com.andrew.apollo.NotificationHelper.java

/**
 * Call this to build the {@link Notification}.
 *///from ww  w .  j av a 2s.c  o m
public void buildNotification(final String albumName, final String artistName, final String trackName,
        final Long albumId, final Bitmap albumArt) {

    // Default notfication layout
    mNotificationTemplate = new RemoteViews(mService.getPackageName(), R.layout.notification_template_base);

    // Set up the content view
    initCollapsedLayout(trackName, artistName, albumArt);

    if (ApolloUtils.hasHoneycomb()) {
        // Notification Builder
        mNotification = new NotificationCompat.Builder(mService).setSmallIcon(R.drawable.stat_notify_music)
                .setContentIntent(getPendingIntent()).setPriority(Notification.PRIORITY_DEFAULT)
                .setContent(mNotificationTemplate).build();
        // Control playback from the notification
        initPlaybackActions();
        if (ApolloUtils.hasJellyBean()) {
            // Expanded notifiction style
            mExpandedView = new RemoteViews(mService.getPackageName(),
                    R.layout.notification_template_expanded_base);
            mNotification.bigContentView = mExpandedView;
            // Control playback from the notification
            initExpandedPlaybackActions();
            // Set up the expanded content view
            initExpandedLayout(trackName, albumName, artistName, albumArt);
        }
        mService.startForeground(APOLLO_MUSIC_SERVICE, mNotification);
    } else {
        // FIXME: I do not understand why this happens, but the
        // NotificationCompat
        // API does not work on Gingerbread. Specifically, {@code
        // #mBuilder.setContent()} won't apply the custom RV in Gingerbread.
        // So,
        // until this is fixed I'll just use the old way.
        mNotification = new Notification();
        mNotification.contentView = mNotificationTemplate;
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        mNotification.icon = R.drawable.stat_notify_music;
        mNotification.contentIntent = getPendingIntent();
        mService.startForeground(APOLLO_MUSIC_SERVICE, mNotification);
    }
}

From source file:com.teclib.service.NotificationGPSActivation.java

public void CustomNotification() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_install_apps);

    Intent intent = new Intent(this, ActiveGPSActivity.class);

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_white_stork).setTicker(getString(R.string.gpsAsk_string)).setOngoing(true)
            .setContentIntent(pIntent).setAutoCancel(true).setContent(remoteViews);

    Notification notificationInstall = builder.build();
    notificationInstall.flags |= Notification.FLAG_AUTO_CANCEL;

    remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_notification_install_apps);

    remoteViews.setTextViewText(R.id.title, getString(R.string.app_name));
    remoteViews.setTextViewText(R.id.text, getString(R.string.gpsAsk_string));

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationmanager.notify(4, builder.build());

}

From source file:com.spollo.player.NotificationHelper.java

/**
 * Call this to build the {@link Notification}.
 *//*  w  w w  .j a  v  a 2s .c om*/
public void buildNotification(final String albumName, final String artistName, final String trackName,
        final Long albumId, final Bitmap albumArt, final boolean isPlaying) {

    // Default notfication layout
    mNotificationTemplate = new RemoteViews(mService.getPackageName(), R.layout.notification_template_base);

    // Set up the content view
    initCollapsedLayout(trackName, artistName, albumArt);

    // Notification Builder
    mNotification = new NotificationCompat.Builder(mService).setSmallIcon(R.drawable.stat_notify_music)
            .setContentIntent(getPendingIntent()).setPriority(Notification.PRIORITY_DEFAULT)
            .setContent(mNotificationTemplate).build();
    // Control playback from the notification
    initPlaybackActions(isPlaying);
    if (ApolloUtils.hasJellyBean()) {
        // Expanded notifiction style
        mExpandedView = new RemoteViews(mService.getPackageName(),
                R.layout.notification_template_expanded_base);
        mNotification.bigContentView = mExpandedView;
        // Control playback from the notification
        initExpandedPlaybackActions(isPlaying);
        // Set up the expanded content view
        initExpandedLayout(trackName, albumName, artistName, albumArt);
    }
    mService.startForeground(SPOLLO_MUSIC_SERVICE, mNotification);
}

From source file:co.carlosjimenez.android.currencyalerts.app.widget.DetailWidgetProvider.java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Perform this loop procedure for each App Widget that belongs to this provider

    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_detail);

        // Create an Intent to launch MainActivity
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent);

        // Set up the header
        setMainCurrencyDetails(context, views, appWidgetIds);

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views, appWidgetIds);
        } else {/*from   ww w.  jav a2  s .  co  m*/
            setRemoteAdapterV11(context, views, appWidgetIds);
        }
        Intent clickIntentTemplate = new Intent(context, DetailActivity.class);

        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate);
        views.setEmptyView(R.id.widget_list, R.id.widget_empty);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

From source file:com.teclib.service.NotificationPasswordPolicies.java

public void CustomNotification() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_install_apps);
    Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_white_stork).setOngoing(true)
            .setTicker(getString(R.string.passwordPoliciesNotification_string)).setContentIntent(pIntent)
            .setContent(remoteViews);/*from  www  . java  2  s . c  o  m*/

    Notification notificationInstall = builder.build();
    notificationInstall.flags |= Notification.FLAG_AUTO_CANCEL;

    remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_notification_install_apps);

    remoteViews.setTextViewText(R.id.title, getString(R.string.app_name));
    remoteViews.setTextViewText(R.id.text, getString(R.string.passwordPoliciesNotification_string));

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationmanager.notify(5, builder.build());

}

From source file:com.boko.vimusic.NotificationHelper.java

/**
 * Call this to build the {@link Notification}.
 *///from ww w .j  a v a2  s.  c o m
public void buildNotification(final String albumName, final String artistName, final String trackName,
        final String albumId, final Bitmap albumArt, final boolean isPlaying) {
    // Default notfication layout
    mNotificationTemplate = new RemoteViews(mService.getPackageName(), R.layout.notification_template_base);

    // Set up the content view
    initCollapsedLayout(trackName, artistName, albumArt);

    // Notification Builder
    mNotification = new NotificationCompat.Builder(mService).setSmallIcon(R.drawable.stat_notify_music)
            .setContentIntent(getPendingIntent()).setPriority(Notification.PRIORITY_DEFAULT)
            .setContent(mNotificationTemplate).build();
    // Control playback from the notification
    initPlaybackActions(isPlaying);
    if (CommonUtils.hasJellyBean()) {
        // Expanded notifiction style
        mExpandedView = new RemoteViews(mService.getPackageName(),
                R.layout.notification_template_expanded_base);
        mNotification.bigContentView = mExpandedView;
        // Control playback from the notification
        initExpandedPlaybackActions(isPlaying);
        // Set up the expanded content view
        initExpandedLayout(trackName, albumName, artistName, albumArt);
    }
    mService.startForeground(APOLLO_MUSIC_SERVICE, mNotification);
}

From source file:com.androidinspain.deskclock.data.StopwatchNotificationBuilder.java

public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*  w ww  .  j a  va2s .c om*/
    final int eventLabel = com.androidinspain.deskclock.R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, StopwatchService.class)
            .setAction(StopwatchService.ACTION_SHOW_STOPWATCH).putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getService(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname,
            com.androidinspain.deskclock.R.layout.chronometer_notif_content);
    content.setChronometer(com.androidinspain.deskclock.R.id.chronometer, base, null, running);

    final List<Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_pause_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(StopwatchService.ACTION_LAP_STOPWATCH)
                    .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

            @DrawableRes
            final int icon2 = com.androidinspain.deskclock.R.drawable.ic_sw_lap_24dp;
            final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(com.androidinspain.deskclock.R.string.sw_notification_lap_number,
                    lapNumber);
            content.setTextViewText(com.androidinspain.deskclock.R.id.state, lap);
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_START_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_start_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_RESET_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon2 = com.androidinspain.deskclock.R.drawable.ic_reset_24dp;
        final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(com.androidinspain.deskclock.R.id.state,
                res.getString(com.androidinspain.deskclock.R.string.swn_paused));
        content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
    }

    final Builder notification = new NotificationCompat.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_stopwatch)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setColor(ContextCompat.getColor(context, com.androidinspain.deskclock.R.color.default_background));

    if (Utils.isNOrLater()) {
        notification.setGroup(nm.getStopwatchNotificationGroupKey());
    }

    for (Action action : actions) {
        notification.addAction(action);
    }

    return notification.build();
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

private Notification createStartingNew() {
    Notification notification = new Notification();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notif_download);

    views.setProgressBar(R.id.progressDownload, 100, 0, true);
    views.setTextViewText(R.id.notifTitle, context.getResources().getString(R.string.Downloading));
    views.setTextViewText(R.id.notifArtist, track.getArtist() + "-" + track.getTitle());
    views.setImageViewResource(R.id.notifPause, R.drawable.widget_pause);

    ComponentName componentName = new ComponentName(context, DownloadingService.class);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(componentName);

    Intent intentPause = new Intent(PlayingService.actionIconPause);
    intentPause.putExtra("aid", track.getAid());
    intentPause.setComponent(componentName);

    views.setOnClickPendingIntent(R.id.notifClose,
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    views.setOnClickPendingIntent(R.id.notifPause,
            PendingIntent.getService(context, notificationId, intentPause, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = views;/*from ww w . j  a v  a 2 s. co m*/
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.download_icon;

    return notification;

}

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

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*from w  w  w.j a  va  2  s .  com*/
    final int eventLabel = R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews collapsed = new RemoteViews(pname, R.layout.stopwatch_notif_collapsed);
    collapsed.setChronometer(R.id.swn_collapsed_chronometer, base, null, running);
    collapsed.setOnClickPendingIntent(R.id.swn_collapsed_hitspace, pendingShowApp);
    collapsed.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch);

    final RemoteViews expanded = new RemoteViews(pname, R.layout.stopwatch_notif_expanded);
    expanded.setChronometer(R.id.swn_expanded_chronometer, base, null, running);
    expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp);
    expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch);

    @IdRes
    final int leftButtonId = R.id.swn_left_button;
    @IdRes
    final int rightButtonId = R.id.swn_right_button;
    if (running) {
        // Left button: Pause
        expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button));
        setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp);
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingPause = Utils.pendingServiceIntent(context, pause);
        expanded.setOnClickPendingIntent(leftButtonId, pendingPause);

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_lap_button));
            setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_sw_lap_24dp);

            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
            final PendingIntent pendingLap = Utils.pendingServiceIntent(context, lap);
            expanded.setOnClickPendingIntent(rightButtonId, pendingLap);
            expanded.setViewVisibility(rightButtonId, VISIBLE);
        } else {
            expanded.setViewVisibility(rightButtonId, INVISIBLE);
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber);
            collapsed.setTextViewText(R.id.swn_collapsed_laps, lap);
            collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE);
            expanded.setTextViewText(R.id.swn_expanded_laps, lap);
            expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE);
        } else {
            collapsed.setViewVisibility(R.id.swn_collapsed_laps, GONE);
            expanded.setViewVisibility(R.id.swn_expanded_laps, GONE);
        }
    } else {
        // Left button: Start
        expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_start_button));
        setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp);
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingStart = Utils.pendingServiceIntent(context, start);
        expanded.setOnClickPendingIntent(leftButtonId, pendingStart);

        // Right button: Reset (dismisses notification and resets stopwatch)
        expanded.setViewVisibility(rightButtonId, VISIBLE);
        expanded.setTextViewText(rightButtonId, res.getText(R.string.sw_reset_button));
        setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp);
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
        final PendingIntent pendingReset = Utils.pendingServiceIntent(context, reset);
        expanded.setOnClickPendingIntent(rightButtonId, pendingReset);

        // Indicate the stopwatch is paused.
        collapsed.setTextViewText(R.id.swn_collapsed_laps, res.getString(R.string.swn_paused));
        collapsed.setViewVisibility(R.id.swn_collapsed_laps, VISIBLE);
        expanded.setTextViewText(R.id.swn_expanded_laps, res.getString(R.string.swn_paused));
        expanded.setViewVisibility(R.id.swn_expanded_laps, VISIBLE);
    }

    // Swipe away will reset the stopwatch without bringing forward the app.
    final Intent reset = new Intent(context, StopwatchService.class)
            .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final Notification notification = new NotificationCompat.Builder(context).setLocalOnly(true)
            .setOngoing(running).setContent(collapsed).setAutoCancel(stopwatch.isPaused())
            .setPriority(NotificationCompat.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch)
            .setDeleteIntent(Utils.pendingServiceIntent(context, reset))
            .setColor(ContextCompat.getColor(context, R.color.default_background)).build();
    notification.bigContentView = expanded;
    return notification;
}