Example usage for android.widget RemoteViews setTextViewText

List of usage examples for android.widget RemoteViews setTextViewText

Introduction

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

Prototype

public void setTextViewText(int viewId, CharSequence text) 

Source Link

Document

Equivalent to calling TextView#setText(CharSequence)

Usage

From source file:com.yamin.kk.service.AudioService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    try {//from  w ww. j  ava 2 s .co  m
        Bitmap cover = AudioUtil.getCover(this, getCurrentMedia(), 64);
        String title = getCurrentMedia().getTitle();
        String artist = getCurrentMedia().getArtist();
        String album = getCurrentMedia().getAlbum();
        Notification notification;

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist).setAutoCancel(false)
                .setOngoing(true);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(MainActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Util.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            Intent iStop = new Intent(ACTION_REMOTE_STOP);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
            if (cover != null)
                view.setImageViewBitmap(R.id.cover, cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause : R.drawable.ic_play);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            if (cover != null)
                view_expanded.setImageViewBitmap(R.id.cover, cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause : R.drawable.ic_play);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(cover).setContentTitle(title)
                    .setContentText(Util.isJellyBeanOrLater() ? artist : getCurrentMedia().getSubtitle())
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, AudioService.class));
        startForeground(3, notification);
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
}

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 ww  w. j av a2 s.  c  om*/
        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.irccloud.android.data.collection.NotificationsList.java

@SuppressLint("NewApi")
private android.app.Notification buildNotification(String ticker, int cid, int bid, long[] eids, String title,
        String text, int count, Intent replyIntent, String network, ArrayList<Notification> messages,
        NotificationCompat.Action otherAction, Bitmap largeIcon, Bitmap wearBackground) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel c = new NotificationChannel(String.valueOf(bid), title,
                NotificationManager.IMPORTANCE_HIGH);
        c.setGroup(String.valueOf(cid));
        ((NotificationManager) IRCCloudApplication.getInstance().getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(c);
    }//from  ww  w .  ja  va2 s.co m
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext());
    int defaults = 0;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            IRCCloudApplication.getInstance().getApplicationContext(), String.valueOf(bid))
                    .setContentTitle(
                            title + ((network != null && !network.equals(title)) ? (" (" + network + ")") : ""))
                    .setContentText(Html.fromHtml(text)).setAutoCancel(true).setTicker(ticker)
                    .setWhen(eids[0] / 1000).setSmallIcon(R.drawable.ic_stat_notify).setLargeIcon(largeIcon)
                    .setColor(IRCCloudApplication.getInstance().getApplicationContext().getResources()
                            .getColor(R.color.ic_background))
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setPriority(hasTouchWiz() ? NotificationCompat.PRIORITY_DEFAULT
                            : NotificationCompat.PRIORITY_HIGH)
                    .setOnlyAlertOnce(false);

    if (ticker != null && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000) {
        String ringtone = prefs.getString("notify_ringtone",
                "android.resource://"
                        + IRCCloudApplication.getInstance().getApplicationContext().getPackageName() + "/"
                        + R.raw.digit);
        if (ringtone.length() > 0)
            builder.setSound(Uri.parse(ringtone));
    }

    int led_color = Integer.parseInt(prefs.getString("notify_led_color", "1"));
    if (led_color == 1) {
        defaults = android.app.Notification.DEFAULT_LIGHTS;
    } else if (led_color == 2) {
        builder.setLights(0xFF0000FF, 500, 500);
    }

    if (prefs.getBoolean("notify_vibrate", true) && ticker != null
            && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000)
        defaults |= android.app.Notification.DEFAULT_VIBRATE;
    else
        builder.setVibrate(new long[] { 0L });

    builder.setDefaults(defaults);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong("lastNotificationTime", System.currentTimeMillis());
    editor.commit();

    Intent i = new Intent();
    i.setComponent(new ComponentName(IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
            "com.irccloud.android.MainActivity"));
    i.putExtra("bid", bid);
    i.setData(Uri.parse("bid://" + bid));
    Intent dismiss = new Intent(IRCCloudApplication.getInstance().getApplicationContext().getResources()
            .getString(R.string.DISMISS_NOTIFICATION));
    dismiss.setData(Uri.parse("irccloud-dismiss://" + bid));
    dismiss.putExtra("bid", bid);
    dismiss.putExtra("eids", eids);

    PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(
            IRCCloudApplication.getInstance().getApplicationContext(), 0, dismiss,
            PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(
            PendingIntent.getActivity(IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                    PendingIntent.FLAG_UPDATE_CURRENT));
    builder.setDeleteIntent(dismissPendingIntent);

    WearableExtender wearableExtender = new WearableExtender();
    wearableExtender.setBackground(wearBackground);
    if (messages != null && messages.size() > 0) {
        StringBuilder weartext = new StringBuilder();
        String servernick = getServerNick(messages.get(0).cid);
        NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(servernick);
        style.setConversationTitle(title + ((network != null) ? (" (" + network + ")") : ""));
        for (Notification n : messages) {
            if (n != null && n.message != null && n.message.length() > 0) {
                if (weartext.length() > 0)
                    weartext.append("<br/>");
                if (n.message_type.equals("buffer_me_msg")) {
                    style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000,
                            " " + ((n.nick == null) ? servernick : n.nick));
                    weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick).append("</b> ")
                            .append(n.message);
                } else {
                    style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000, n.nick);
                    weartext.append("<b>&lt;").append((n.nick == null) ? servernick : n.nick)
                            .append("&gt;</b> ").append(n.message);
                }
            }
        }

        ArrayList<String> history = new ArrayList<>(messages.size());
        for (int j = messages.size() - 1; j >= 0; j--) {
            Notification n = messages.get(j);
            if (n != null) {
                if (n.nick == null)
                    history.add(Html.fromHtml(n.message).toString());
                else
                    break;
            }
        }
        builder.setRemoteInputHistory(history.toArray(new String[history.size()]));
        builder.setStyle(style);

        if (messages.size() > 1) {
            wearableExtender.addPage(
                    new NotificationCompat.Builder(IRCCloudApplication.getInstance().getApplicationContext())
                            .setContentText(Html.fromHtml(weartext.toString()))
                            .extend(new WearableExtender().setStartScrollBottom(true)).build());
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            weartext.setLength(0);
            int j = 0;
            for (Notification n : messages) {
                if (messages.size() - ++j < 3) {
                    if (n != null && n.message != null && n.message.length() > 0) {
                        if (weartext.length() > 0)
                            weartext.append("<br/>");
                        if (n.message_type.equals("buffer_me_msg")) {
                            weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick)
                                    .append("</b> ").append(n.message);
                        } else {
                            weartext.append("<b>&lt;").append((n.nick == null) ? servernick : n.nick)
                                    .append("&gt;</b> ").append(n.message);
                        }
                    }
                }
            }

            RemoteViews bigContentView = new RemoteViews(
                    IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
                    R.layout.notification_expanded);
            bigContentView.setTextViewText(R.id.title,
                    title + (!title.equals(network) ? (" (" + network + ")") : ""));
            bigContentView.setTextViewText(R.id.text, Html.fromHtml(weartext.toString()));
            bigContentView.setImageViewBitmap(R.id.image, largeIcon);
            bigContentView.setLong(R.id.time, "setTime", eids[0] / 1000);
            if (count > 3) {
                bigContentView.setViewVisibility(R.id.more, View.VISIBLE);
                bigContentView.setTextViewText(R.id.more, "+" + (count - 3) + " more");
            } else {
                bigContentView.setViewVisibility(R.id.more, View.GONE);
            }
            if (replyIntent != null && prefs.getBoolean("notify_quickreply", true)) {
                bigContentView.setViewVisibility(R.id.actions, View.VISIBLE);
                bigContentView.setViewVisibility(R.id.action_divider, View.VISIBLE);
                i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(),
                        QuickReplyActivity.class);
                i.setData(Uri.parse("irccloud-bid://" + bid));
                i.putExtras(replyIntent);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent quickReplyIntent = PendingIntent.getActivity(
                        IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                bigContentView.setOnClickPendingIntent(R.id.action_reply, quickReplyIntent);
            }
            builder.setCustomBigContentView(bigContentView);
        }
    }

    if (replyIntent != null) {
        PendingIntent replyPendingIntent = PendingIntent.getService(
                IRCCloudApplication.getInstance().getApplicationContext(), bid + 1, replyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.addAction(new NotificationCompat.Action.Builder(0, "Reply", replyPendingIntent)
                    .setAllowGeneratedReplies(true)
                    .addRemoteInput(
                            new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build())
                    .build());
        }

        NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(
                R.drawable.ic_wearable_reply, "Reply", replyPendingIntent).setAllowGeneratedReplies(true)
                        .addRemoteInput(
                                new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build());

        NotificationCompat.Action.WearableExtender actionExtender = new NotificationCompat.Action.WearableExtender()
                .setHintLaunchesActivity(true).setHintDisplayActionInline(true);

        wearableExtender.addAction(actionBuilder.extend(actionExtender).build());

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                title + ((network != null) ? (" (" + network + ")") : ""))
                        .setReadPendingIntent(dismissPendingIntent).setReplyAction(replyPendingIntent,
                                new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build());

        if (messages != null) {
            for (Notification n : messages) {
                if (n != null && n.nick != null && n.message != null && n.message.length() > 0) {
                    if (n.buffer_type.equals("conversation")) {
                        if (n.message_type.equals("buffer_me_msg"))
                            unreadConvBuilder
                                    .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString());
                        else
                            unreadConvBuilder.addMessage(Html.fromHtml(n.message).toString());
                    } else {
                        if (n.message_type.equals("buffer_me_msg"))
                            unreadConvBuilder
                                    .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString());
                        else
                            unreadConvBuilder
                                    .addMessage(n.nick + " said: " + Html.fromHtml(n.message).toString());
                    }
                }
            }
        } else {
            unreadConvBuilder.addMessage(text);
        }
        unreadConvBuilder.setLatestTimestamp(eids[count - 1] / 1000);

        builder.extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConvBuilder.build()));
    }

    if (replyIntent != null && prefs.getBoolean("notify_quickreply", true)
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(), QuickReplyActivity.class);
        i.setData(Uri.parse("irccloud-bid://" + bid));
        i.putExtras(replyIntent);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent quickReplyIntent = PendingIntent.getActivity(
                IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(R.drawable.ic_action_reply, "Quick Reply", quickReplyIntent);
    }

    if (otherAction != null) {
        int drawable = 0;
        if (otherAction.getIcon() == R.drawable.ic_wearable_add)
            drawable = R.drawable.ic_action_add;
        else if (otherAction.getIcon() == R.drawable.ic_wearable_reply)
            drawable = R.drawable.ic_action_reply;
        builder.addAction(
                new NotificationCompat.Action(drawable, otherAction.getTitle(), otherAction.getActionIntent()));
        wearableExtender.addAction(otherAction);
    }

    builder.extend(wearableExtender);

    return builder.build();
}

From source file:com.nogago.android.tracks.widgets.TrackWidgetProvider.java

/**
 * Updates view./*from w w  w.jav a  2  s  .c o m*/
 * 
 * @param remoteViews the remote views
 */
private void updateView(RemoteViews remoteViews) {
    Track track = selectedTrackId != PreferencesUtils.SELECTED_TRACK_ID_DEFAULT
            ? myTracksProviderUtils.getTrack(selectedTrackId)
            : myTracksProviderUtils.getLastTrack();
    TripStatistics tripStatistics = track == null ? null : track.getTripStatistics();
    String distance = tripStatistics == null ? unknown
            : StringUtils.formatDistance(context, tripStatistics.getTotalDistance(), metricUnits);
    int timeLabelId = useTotalTime ? R.string.stats_total_time : R.string.stats_moving_time;
    String time = tripStatistics == null ? unknown
            : StringUtils.formatElapsedTime(
                    useTotalTime ? tripStatistics.getTotalTime() : tripStatistics.getMovingTime());
    int speedLabelId;
    if (useTotalTime) {
        speedLabelId = reportSpeed ? R.string.stats_average_speed : R.string.stats_average_pace;
    } else {
        speedLabelId = reportSpeed ? R.string.stats_average_moving_speed : R.string.stats_average_moving_pace;
    }

    String speed = tripStatistics == null ? unknown
            : StringUtils.formatSpeed(context,
                    useTotalTime ? tripStatistics.getAverageSpeed() : tripStatistics.getAverageMovingSpeed(),
                    metricUnits, reportSpeed);
    remoteViews.setTextViewText(R.id.track_widget_distance_value, distance);
    remoteViews.setTextViewText(R.id.track_widget_time_label, context.getString(timeLabelId));
    remoteViews.setTextViewText(R.id.track_widget_time_value, time);
    remoteViews.setTextViewText(R.id.track_widget_speed_label, context.getString(speedLabelId));
    remoteViews.setTextViewText(R.id.track_widget_speed_value, speed);

    Intent intent;
    if (track != null) {
        intent = IntentUtils.newIntent(context, TrackDetailActivity.class)
                .putExtra(TrackDetailActivity.EXTRA_TRACK_ID, track.getId());
    } else {
        intent = IntentUtils.newIntent(context, TrackListActivity.class);
    }
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.from(context);
    taskStackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, 0);
    remoteViews.setOnClickPendingIntent(R.id.track_widget_statistics, pendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.track_widget_icon, pendingIntent);
}

From source file:com.darshancomputing.BatteryIndicatorPro.BatteryInfoService.java

private void updateWidgets(BatteryInfo info) {
    //Intent mainWindowIntent = new Intent(this, BatteryInfoActivity.class);
    //PendingIntent mainWindowPendingIntent = PendingIntent.getActivity(this, RC_MAIN, mainWindowIntent, 0);
    //PendingIntent currentInfoPendingIntent = PendingIntent.getActivity(this, RC_MAIN, currentInfoIntent, 0);

    if (info == null) {
        cwbg.setLevel(0);//from  ww  w  . j a v a2s.c  o m
    } else {
        bl.setLevel(info.percent);
        cwbg.setLevel(info.percent);
    }

    for (Integer widgetId : widgetIds) {
        RemoteViews rv;

        android.appwidget.AppWidgetProviderInfo awpInfo = widgetManager.getAppWidgetInfo(widgetId);
        if (awpInfo == null)
            continue; // Based on Developer Console crash reports, this can be null sometimes

        int initLayout = awpInfo.initialLayout;

        if (initLayout == R.layout.circle_app_widget) {
            rv = new RemoteViews(getPackageName(), R.layout.circle_app_widget);
            rv.setImageViewBitmap(R.id.circle_widget_image_view, cwbg.getBitmap());
        } else {
            rv = new RemoteViews(getPackageName(), R.layout.full_app_widget);

            if (info == null) {
                rv.setImageViewBitmap(R.id.battery_level_view, cwbg.getBitmap());
                rv.setTextViewText(R.id.fully_charged, "");
                rv.setTextViewText(R.id.time_remaining, "");
                rv.setTextViewText(R.id.until_what, "");
            } else {
                rv.setImageViewBitmap(R.id.battery_level_view, bl.getBitmap());

                if (info.prediction.what == BatteryInfo.Prediction.NONE) {
                    rv.setTextViewText(R.id.fully_charged, str.timeRemaining(info));
                    rv.setTextViewText(R.id.time_remaining, "");
                    rv.setTextViewText(R.id.until_what, "");
                } else {
                    rv.setTextViewText(R.id.fully_charged, "");
                    rv.setTextViewText(R.id.time_remaining, str.timeRemaining(info));
                    rv.setTextViewText(R.id.until_what, str.untilWhat(info));
                }
            }
        }

        if (info == null)
            rv.setTextViewText(R.id.level, "XX" + str.percent_symbol);
        else
            rv.setTextViewText(R.id.level, "" + info.percent + str.percent_symbol);

        rv.setOnClickPendingIntent(R.id.widget_layout, currentInfoPendingIntent);
        widgetManager.updateAppWidget(widgetId, rv);
    }
}

From source file:org.videolan.vlc.audio.AudioService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    try {//from   w  w  w.j ava  2  s .c o m
        Media media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = media.getArtist();
        String album = media.getAlbum();
        Notification notification;

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist).setAutoCancel(false)
                .setOngoing(true);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(MainActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (LibVlcUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            Intent iStop = new Intent(ACTION_REMOTE_STOP);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
            if (cover != null)
                view.setImageViewBitmap(R.id.cover, cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            if (cover != null)
                view_expanded.setImageViewBitmap(R.id.cover, cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(cover).setContentTitle(title)
                    .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle())
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, AudioService.class));
        startForeground(3, notification);
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
}

From source file:github.madmarty.madsonic.util.Util.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing) {

    // Use the same text for the ticker and the expanded notification
    String title = song.getTitle();
    String arist = song.getArtist();
    String album = song.getAlbum();

    // Set the album art.
    try {//from  ww w.j  a v  a  2  s. c  o m
        int size = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
        Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, size);
        if (bitmap == null) {
            // set default album art
            rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            rv.setImageViewBitmap(R.id.notification_image, bitmap);
        }
    } catch (Exception x) {
        LOG.warn("Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    rv.setImageViewResource(R.id.control_starred,
            song.isStarred() ? android.R.drawable.btn_star_big_on : android.R.drawable.btn_star_big_off);

    // set the text for the notifications
    rv.setTextViewText(R.id.notification_title, title);
    rv.setTextViewText(R.id.notification_artist, arist);
    rv.setTextViewText(R.id.notification_album, album);

    Pair<Integer, Integer> colors = getNotificationTextColors(context);
    if (colors.getFirst() != null) {
        rv.setTextColor(R.id.notification_title, colors.getFirst());
    }
    if (colors.getSecond() != null) {
        rv.setTextColor(R.id.notification_artist, colors.getSecond());
    }

    if (!playing) {
        rv.setImageViewResource(R.id.control_pause, R.drawable.notification_play);
        rv.setImageViewResource(R.id.control_previous, R.drawable.notification_stop);
    }

    // Create actions for media buttons
    PendingIntent pendingIntent;
    if (playing) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    } else {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    }

    Intent starredIntent = new Intent("KEYCODE_MEDIA_STARRED");
    starredIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    starredIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_STAR));
    pendingIntent = PendingIntent.getService(context, 0, starredIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_starred, pendingIntent);

    Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
    pauseIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_pause, pendingIntent);

    Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
    nextIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    nextIntent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_next, pendingIntent);
}

From source file:com.dzt.musicplay.player.AudioService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    Constant.print_i("AudioService----------->showNotification");
    try {//w  w  w.j a v a2s  . c om
        Media media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = media.getArtist();
        String album = media.getAlbum();
        Notification notification;

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_music).setTicker(title + " - " + artist).setAutoCancel(false)
                .setOngoing(true);

        //Activity
        Intent notificationIntent = new Intent(this, MusicPlayActivity.class);
        notificationIntent.setAction(MusicPlayActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (LibVlcUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            Intent iStop = new Intent(ACTION_REMOTE_STOP);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
            if (cover != null)
                view.setImageViewBitmap(R.id.cover, cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            if (cover != null)
                view_expanded.setImageViewBitmap(R.id.cover, cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(cover).setContentTitle(title)
                    .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle())
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, AudioService.class));
        startForeground(3, notification);
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
    Constant.print_i("AudioService----------->showNotification----end");
}

From source file:com.kasungunathilaka.sarigama.service.PlayerService.java

private void setupNotification(Song song, String notificationType) {
    Bitmap remote_picture = null;/*from   w  w w.j  a  v a 2  s. co m*/
    RemoteViews smallNotification;
    RemoteViews bigNotification;
    Notification notification;

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    if (song != null) {
        if (song.getSongImage().contentEquals("http://sarigama.lk/img/songs/default.jpg")) {
            remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(),
                    R.drawable.default_song_art);
        } else {
            try {
                remote_picture = BitmapFactory
                        .decodeStream((InputStream) new URL(song.getSongImage()).getContent());
            } catch (Exception e) {
                remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(),
                        R.drawable.default_song_art);
                e.printStackTrace();
            }
        }
    }

    switch (notificationType) {
    case HomeActivity.NOTIFICATION_TYPE_PLAY:
        try {
            smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small);
            smallNotification.setImageViewBitmap(R.id.ivSmallNotificationSongImage, remote_picture);
            smallNotification.setTextViewText(R.id.tvSmallNotificationSong, song.getSongTitle());
            smallNotification.setTextViewText(R.id.tvSmallNotificationArtist, song.getSongArtist());
            smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_pause_white);
            smallNotification.setOnClickPendingIntent(R.id.flSmallPlay, playIntent);
            smallNotification.setOnClickPendingIntent(R.id.ivSmallNext, nextIntent);

            bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big);
            bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture);
            bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle());
            bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist());
            bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_pause_white);
            bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent);

            notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_play_white)
                    .setAutoCancel(false).setOngoing(true).setContent(smallNotification)
                    .setContentIntent(startIntent).build();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notification.bigContentView = bigNotification;
            }
            notificationManager.notify(NOTIFICATION_ID, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    case HomeActivity.NOTIFICATION_TYPE_PAUSE:
        try {
            smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small);
            smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_play_white);

            bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big);
            bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture);
            bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle());
            bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist());
            bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_play_white);
            bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent);
            bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent);

            notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_pause_white)
                    .setOngoing(false).setAutoCancel(false).setContent(smallNotification)
                    .setContentIntent(startIntent).build();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                notification.bigContentView = bigNotification;
            }
            notificationManager.notify(NOTIFICATION_ID, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    case HomeActivity.NOTIFICATION_TYPE_DISMISS:
        notificationManager.cancel(NOTIFICATION_ID);
        break;
    }
}

From source file:org.videolan.vlc.PlaybackService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void showNotification() {
    try {/*www .j  ava 2s .  c  o  m*/
        MediaWrapper media = getCurrentMedia();
        if (media == null)
            return;
        Bitmap cover = AudioUtil.getCover(this, media, 64);
        String title = media.getTitle();
        String artist = Util.getMediaArtist(this, media);
        String album = Util.getMediaAlbum(this, media);
        Notification notification;

        if (media.isArtistUnknown() && media.isAlbumUnknown() && media.getNowPlaying() != null) {
            artist = media.getNowPlaying();
            album = "";
        }

        //Watch notification dismissed
        PendingIntent piStop = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_REMOTE_STOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

        // add notification to status bar
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist)
                .setAutoCancel(!MediaPlayer().isPlaying()).setOngoing(MediaPlayer().isPlaying())
                .setDeleteIntent(piStop);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(AudioPlayerContainerActivity.ACTION_SHOW_PLAYER);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra(START_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (AndroidUtil.isJellyBeanOrLater()) {
            Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
            Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
            Intent iForward = new Intent(ACTION_REMOTE_FORWARD);
            PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews view = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification);
            view.setImageViewBitmap(R.id.cover,
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover);
            view.setTextViewText(R.id.songName, title);
            view.setTextViewText(R.id.artist, artist);
            view.setImageViewResource(R.id.play_pause,
                    MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view.setOnClickPendingIntent(R.id.forward, piForward);
            view.setOnClickPendingIntent(R.id.stop, piStop);
            view.setOnClickPendingIntent(R.id.content, pendingIntent);

            RemoteViews view_expanded = new RemoteViews(BuildConfig.APPLICATION_ID,
                    R.layout.notification_expanded);
            view_expanded.setImageViewBitmap(R.id.cover,
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover);
            view_expanded.setTextViewText(R.id.songName, title);
            view_expanded.setTextViewText(R.id.artist, artist);
            view_expanded.setTextViewText(R.id.album, album);
            view_expanded.setImageViewResource(R.id.play_pause,
                    MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w);
            view_expanded.setOnClickPendingIntent(R.id.backward, piBackward);
            view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay);
            view_expanded.setOnClickPendingIntent(R.id.forward, piForward);
            view_expanded.setOnClickPendingIntent(R.id.stop, piStop);
            view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent);

            if (AndroidUtil.isLolliPopOrLater()) {
                //Hide stop button on pause, we swipe notification to stop
                view.setViewVisibility(R.id.stop, MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE);
                view_expanded.setViewVisibility(R.id.stop,
                        MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE);
                //Make notification appear on lockscreen
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            }

            notification = builder.build();
            notification.contentView = view;
            notification.bigContentView = view_expanded;
        } else {
            builder.setLargeIcon(
                    cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover)
                    .setContentTitle(title)
                    .setContentText(
                            AndroidUtil.isJellyBeanOrLater() ? artist : Util.getMediaSubtitle(this, media))
                    .setContentInfo(album).setContentIntent(pendingIntent);
            notification = builder.build();
        }

        startService(new Intent(this, PlaybackService.class));
        if (!AndroidUtil.isLolliPopOrLater() || MediaPlayer().isPlaying())
            startForeground(3, notification);
        else {
            stopForeground(false);
            NotificationManagerCompat.from(this).notify(3, notification);
        }
    } catch (NoSuchMethodError e) {
        // Compat library is wrong on 3.2
        // http://code.google.com/p/android/issues/detail?id=36359
        // http://code.google.com/p/android/issues/detail?id=36502
    }
}