Example usage for android.widget RemoteViews setOnClickPendingIntent

List of usage examples for android.widget RemoteViews setOnClickPendingIntent

Introduction

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

Prototype

public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) 

Source Link

Document

Equivalent to calling android.view.View#setOnClickListener(android.view.View.OnClickListener) to launch the provided PendingIntent .

Usage

From source file:com.kiandastream.musicplayer.MusicService.java

public void setListeners(RemoteViews view, String songname) {
    //listener 1//from   ww  w  . j a v a2s .  c om
    view.setTextViewText(R.id.songname, songname);
    PendingIntent backword = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_REWIND), 0);
    view.setOnClickPendingIntent(R.id.backword, backword);

    //listener 2
    if (mState == State.Playing) {

        PendingIntent playpause = PendingIntent.getService(this, 1, new Intent(MusicService.ACTION_PAUSE), 0);
        view.setOnClickPendingIntent(R.id.play, playpause);
        view.setImageViewResource(R.id.play, R.drawable.pause);
    } else {
        if (mState == State.Paused) {
            PendingIntent playpause = PendingIntent.getService(this, 1, new Intent(MusicService.ACTION_RESUME),
                    0);
            view.setOnClickPendingIntent(R.id.play, playpause);
            view.setImageViewResource(R.id.play, R.drawable.play);
        }

    }
    //listener 3

    PendingIntent forward = PendingIntent.getService(this, 2, new Intent(MusicService.ACTION_NEXT), 0);
    view.setOnClickPendingIntent(R.id.forward, forward);

    //listener 4

    PendingIntent close = PendingIntent.getService(this, 3, new Intent(MusicService.ACTION_STOP), 0);
    view.setOnClickPendingIntent(R.id.close, close);
}

From source file:com.battlelancer.seriesguide.appwidget.ListWidgetProvider.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static RemoteViews buildRemoteViews(Context context, AppWidgetManager appWidgetManager,
        int appWidgetId) {
    // setup intent pointing to RemoteViewsService providing the views for the collection
    Intent intent = new Intent(context, ListWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    // When intents are compared, the extras are ignored, so we need to
    // embed the extras into the data so that the extras will not be
    // ignored.//w w  w.j a  v a 2 s .  co  m
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

    // determine layout (current size) and theme (user pref)
    final boolean isCompactLayout = isCompactLayout(appWidgetManager, appWidgetId);
    final boolean isLightTheme = WidgetSettings.isLightTheme(context, appWidgetId);
    int layoutResId;
    if (isLightTheme) {
        layoutResId = isCompactLayout ? R.layout.appwidget_v11_light_compact : R.layout.appwidget_v11_light;
    } else {
        layoutResId = isCompactLayout ? R.layout.appwidget_v11_compact : R.layout.appwidget_v11;
    }

    // build widget views
    RemoteViews rv = new RemoteViews(context.getPackageName(), layoutResId);
    rv.setRemoteAdapter(R.id.list_view, intent);
    // The empty view is displayed when the collection has no items. It
    // should be a sibling of the collection view.
    rv.setEmptyView(R.id.list_view, R.id.empty_view);

    // set the background color
    int bgColor = WidgetSettings.getWidgetBackgroundColor(context, appWidgetId, isLightTheme);
    rv.setInt(R.id.container, "setBackgroundColor", bgColor);

    // determine type specific values
    final int widgetType = WidgetSettings.getWidgetListType(context, appWidgetId);
    int showsTabIndex;
    int titleResId;
    int emptyResId;
    if (widgetType == WidgetSettings.Type.UPCOMING) {
        // upcoming
        showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_UPCOMING;
        titleResId = R.string.upcoming;
        emptyResId = R.string.noupcoming;
    } else if (widgetType == WidgetSettings.Type.RECENT) {
        // recent
        showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_RECENT;
        titleResId = R.string.recent;
        emptyResId = R.string.norecent;
    } else {
        // favorites
        showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_SHOWS;
        titleResId = R.string.action_shows_filter_favorites;
        emptyResId = R.string.no_nextepisode;
    }

    // change title and empty view based on type
    rv.setTextViewText(R.id.empty_view, context.getString(emptyResId));
    if (!isCompactLayout) {
        // only regular layout has text title
        rv.setTextViewText(R.id.widgetTitle, context.getString(titleResId));
    }

    // app launch button
    final Intent appLaunchIntent = new Intent(context, ShowsActivity.class)
            .putExtra(ShowsActivity.InitBundle.SELECTED_TAB, showsTabIndex);
    PendingIntent pendingIntent = TaskStackBuilder.create(context).addNextIntent(appLaunchIntent)
            .getPendingIntent(appWidgetId, PendingIntent.FLAG_UPDATE_CURRENT);
    rv.setOnClickPendingIntent(R.id.widget_title, pendingIntent);

    // item intent template, launches episode detail view
    TaskStackBuilder builder = TaskStackBuilder.create(context);
    builder.addNextIntent(appLaunchIntent);
    builder.addNextIntent(new Intent(context, EpisodesActivity.class));
    rv.setPendingIntentTemplate(R.id.list_view, builder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT));

    // settings button
    Intent settingsIntent = new Intent(context, ListWidgetConfigure.class)
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
            .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    rv.setOnClickPendingIntent(R.id.widget_settings,
            PendingIntent.getActivity(context, appWidgetId, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    return rv;
}

From source file:eu.power_switch.widget.provider.ReceiverWidgetProvider.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d("Updating Receiver Widgets...");
    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];
        RemoteViews remoteViews = new RemoteViews(
                context.getResources().getString(eu.power_switch.shared.R.string.PACKAGE_NAME),
                R.layout.widget_receiver);

        try {// w ww  .jav  a 2s. c  o  m
            ReceiverWidget receiverWidget = DatabaseHandler.getReceiverWidget(appWidgetId);
            Room room = DatabaseHandler.getRoom(receiverWidget.getRoomId());
            if (room != null) {
                Receiver receiver = DatabaseHandler.getReceiver(receiverWidget.getReceiverId());

                if (receiver != null) {
                    Apartment apartment = DatabaseHandler.getApartment(room.getApartmentId());
                    // update UI
                    remoteViews.setTextViewText(R.id.textView_receiver_widget_name,
                            apartment.getName() + ": " + room.getName() + ": " + receiver.getName());

                    LinkedList<Button> buttons = receiver.getButtons();

                    // remove all previous buttons
                    remoteViews.removeAllViews(R.id.linearlayout_receiver_widget);

                    // add buttons from database
                    int buttonOffset = 0;
                    for (Button button : buttons) {
                        // set button action
                        RemoteViews buttonView = new RemoteViews(
                                context.getResources().getString(eu.power_switch.shared.R.string.PACKAGE_NAME),
                                R.layout.widget_receiver_button_layout);
                        SpannableString s = new SpannableString(button.getName());
                        s.setSpan(new StyleSpan(Typeface.BOLD), 0, button.getName().length(), 0);
                        buttonView.setTextViewText(R.id.button_widget_universal, s);
                        if (SmartphonePreferencesHandler.getHighlightLastActivatedButton()
                                && receiver.getLastActivatedButtonId().equals(button.getId())) {
                            buttonView.setTextColor(R.id.button_widget_universal,
                                    ContextCompat.getColor(context, R.color.color_light_blue_a700));
                        }

                        PendingIntent intent = WidgetIntentReceiver.buildReceiverWidgetActionPendingIntent(
                                context, apartment, room, receiver, button, appWidgetId * 15 + buttonOffset);
                        buttonView.setOnClickPendingIntent(R.id.button_widget_universal, intent);

                        remoteViews.addView(R.id.linearlayout_receiver_widget, buttonView);
                        buttonOffset++;
                    }
                    remoteViews.setViewVisibility(R.id.linearlayout_receiver_widget, View.VISIBLE);
                } else {
                    remoteViews.setTextViewText(R.id.textView_receiver_widget_name,
                            context.getString(R.string.receiver_not_found));
                    remoteViews.removeAllViews(R.id.linearlayout_receiver_widget);
                    remoteViews.setViewVisibility(R.id.linearlayout_receiver_widget, View.GONE);
                }
            } else {
                remoteViews.setTextViewText(R.id.textView_receiver_widget_name,
                        context.getString(R.string.room_not_found));
                remoteViews.removeAllViews(R.id.linearlayout_receiver_widget);
                remoteViews.setViewVisibility(R.id.linearlayout_receiver_widget, View.GONE);
            }
        } catch (Exception e) {
            Log.e(e);
            remoteViews.setTextViewText(R.id.textView_receiver_widget_name,
                    context.getString(R.string.unknown_error));
            remoteViews.removeAllViews(R.id.linearlayout_receiver_widget);
            remoteViews.setViewVisibility(R.id.linearlayout_receiver_widget, View.GONE);
        }
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

From source file:li.klass.fhem.appwidget.view.widget.medium.TargetStateWidgetView.java

@Override
protected void fillWidgetView(Context context, RemoteViews view, FhemDevice<?> device,
        WidgetConfiguration widgetConfiguration) {
    String payload = widgetConfiguration.payload.get(1);
    String state = device.getEventMapStateFor(payload);

    view.setTextViewText(R.id.button, state);

    PendingIntent pendingIntent;//  ww  w . j ava 2s  .  c  om
    if (requiresAdditionalInformation(state)) {
        Intent actionIntent = new Intent(context, TargetStateAdditionalInformationActivity.class);
        actionIntent.putExtra(BundleExtraKeys.DEVICE_NAME, device.getName());
        actionIntent.putExtra(BundleExtraKeys.DEVICE_TARGET_STATE, payload);
        actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        pendingIntent = PendingIntent.getActivity(context, widgetConfiguration.widgetId, actionIntent,
                FLAG_UPDATE_CURRENT);
    } else {
        Intent actionIntent = new Intent(Actions.DEVICE_SET_STATE);
        actionIntent.setClass(context, DeviceIntentService.class);
        actionIntent.putExtra(BundleExtraKeys.DEVICE_NAME, device.getName());
        actionIntent.putExtra(BundleExtraKeys.DEVICE_TARGET_STATE, payload);

        pendingIntent = PendingIntent.getService(context, widgetConfiguration.widgetId, actionIntent,
                FLAG_UPDATE_CURRENT);
    }

    view.setOnClickPendingIntent(R.id.button, pendingIntent);

    openDeviceDetailPageWhenClicking(R.id.main, view, device, widgetConfiguration, context);
}

From source file:eu.power_switch.widget.activity.ConfigureReceiverWidgetActivity.java

private void saveCurrentConfiguration() {
    try {//from   w  w w.  j  a v  a  2  s.  c om
        // First, get the App Widget ID from the Intent that launched the Activity:
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
            int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            // Perform your App Widget configuration:
            Apartment selectedApartment = getSelectedApartment();
            Room selectedRoom = selectedApartment.getRoom(spinnerRoom.getSelectedItem().toString());
            Receiver selectedReceiver = selectedRoom.getReceiver(spinnerReceiver.getSelectedItem().toString());

            // save new widget data to database
            ReceiverWidget receiverWidget = new ReceiverWidget(appWidgetId, selectedRoom.getId(),
                    selectedReceiver.getId());
            DatabaseHandler.addReceiverWidget(receiverWidget);
            // When the configuration is complete, get an instance of
            // the AppWidgetManager by calling getInstance(Context):
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(ConfigureReceiverWidgetActivity.this);
            // Update the App Widget with a RemoteViews layout by
            // calling updateAppWidget(int, RemoteViews):
            RemoteViews remoteViews = new RemoteViews(getString(eu.power_switch.shared.R.string.PACKAGE_NAME),
                    R.layout.widget_receiver);

            LinkedList<Button> buttons = selectedReceiver.getButtons();

            remoteViews.setTextViewText(R.id.textView_receiver_widget_name, selectedApartment.getName() + ": "
                    + selectedRoom.getName() + ": " + selectedReceiver.getName());

            int buttonOffset = 0;
            for (Button button : buttons) {
                // set button action
                RemoteViews buttonView = new RemoteViews(
                        getString(eu.power_switch.shared.R.string.PACKAGE_NAME),
                        R.layout.widget_receiver_button_layout);
                SpannableString s = new SpannableString(button.getName());
                s.setSpan(new StyleSpan(Typeface.BOLD), 0, button.getName().length(), 0);
                buttonView.setTextViewText(R.id.button_widget_universal, s);

                if (SmartphonePreferencesHandler.getHighlightLastActivatedButton()
                        && selectedReceiver.getLastActivatedButtonId().equals(button.getId())) {
                    buttonView.setTextColor(R.id.button_widget_universal,
                            ContextCompat.getColor(getApplicationContext(), R.color.color_light_blue_a700));
                }

                PendingIntent pendingIntent = WidgetIntentReceiver.buildReceiverWidgetActionPendingIntent(
                        getApplicationContext(), selectedApartment, selectedRoom, selectedReceiver, button,
                        appWidgetId * 15 + buttonOffset);

                buttonView.setOnClickPendingIntent(R.id.button_widget_universal, pendingIntent);

                remoteViews.addView(R.id.linearlayout_receiver_widget, buttonView);
                buttonOffset++;
            }

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

            // Finally, create the return Intent, set it with the
            // Activity result, and finish the Activity:
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    } catch (Exception e) {
        StatusMessageHandler.showErrorMessage(this, e);
    }
}

From source file:com.namelessdev.mpdroid.NotificationService.java

/**
 * buildExpandedNotification builds upon the collapsed notification resources to create
 * the resources necessary for the expanded notification RemoteViews.
 *
 * @return The expanded notification RemoteViews.
 *//*from w  w  w  . j  ava  2  s . c  o  m*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private RemoteViews buildExpandedNotification() {
    final RemoteViews resultView;

    if (mNotification == null || mNotification.bigContentView == null) {
        resultView = new RemoteViews(getPackageName(), R.layout.notification_big);
    } else {
        resultView = mNotification.bigContentView;
    }

    buildBaseNotification(resultView);

    /** When streaming, move things down (hopefully, very) temporarily. */
    if (mediaPlayerServiceIsBuffering) {
        resultView.setTextViewText(R.id.notificationAlbum, mCurrentMusic.getArtist());
    } else {
        resultView.setTextViewText(R.id.notificationAlbum, mCurrentMusic.getAlbum());
    }

    resultView.setOnClickPendingIntent(R.id.notificationPrev, notificationPrevious);

    return resultView;
}

From source file:name.gumartinm.weather.information.widget.WidgetIntentService.java

private RemoteViews makeErrorView(final int appWidgetId) {
    final RemoteViews remoteView = new RemoteViews(this.getApplicationContext().getPackageName(),
            R.layout.appwidget_error);/*www.  ja v a 2s.  co  m*/

    // 6. Activity launcher.
    final Intent resultIntent = new Intent(this.getApplicationContext(), WidgetConfigure.class);
    resultIntent.putExtra("actionFromUser", true);
    resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    // From: http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
    final Uri data = Uri.withAppendedPath(Uri.parse("PAIN" + "://widget/id/"), String.valueOf(appWidgetId));
    resultIntent.setData(data);

    final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(WidgetConfigure.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent);

    return remoteView;
}

From source file:ro.ciubex.keepscreenlock.MainApplication.java

/**
 * Build the notification item./*from  w  ww .java 2  s .  c  o  m*/
 *
 * @param allowDismiss Flag used to create a dismissible notification.
 * @return The notification item.
 */
private Notification getNotification(boolean allowDismiss) {
    if (mNotification == null) {
        PendingIntent pendingScreenLockIntent = PendingIntent.getBroadcast(this, 0,
                getNotificationScreenLockIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        remoteViews.setOnClickPendingIntent(R.id.lockScreenView, pendingScreenLockIntent);

        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
        notifBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        notifBuilder.setSmallIcon(R.mipmap.ic_launcher_red);
        notifBuilder.setContent(remoteViews);

        notifBuilder.setContentTitle(getString(R.string.app_name));
        notifBuilder.setContentText(getString(R.string.notification_text));

        mNotification = notifBuilder.build();
    }
    if (isNotificationAlwaysDismissible() || allowDismiss) {
        mNotification.flags &= ~Notification.FLAG_NO_CLEAR;
    } else {
        mNotification.flags |= Notification.FLAG_NO_CLEAR;
    }
    return mNotification;
}

From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.UrlDeviceDiscoveryService.java

private void updateSummaryNotificationRemoteViewsFirstBeacon(PwPair pwPair, RemoteViews remoteViews) {
    PwsResult pwsResult = pwPair.getPwsResult();
    remoteViews.setImageViewBitmap(R.id.icon_firstBeacon, Utils.getBitmapIcon(mPwCollection, pwsResult));
    remoteViews.setTextViewText(R.id.title_firstBeacon, pwsResult.getTitle());
    remoteViews.setTextViewText(R.id.url_firstBeacon, pwsResult.getSiteUrl());
    remoteViews.setTextViewText(R.id.description_firstBeacon, pwsResult.getDescription());
    // Recolor notifications to have light text for non-Lollipop devices
    if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
        remoteViews.setTextColor(R.id.title_firstBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR);
        remoteViews.setTextColor(R.id.url_firstBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR);
        remoteViews.setTextColor(R.id.description_firstBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR);
    }//  w w w  .  j a  va 2 s.  c  o m

    // Create an intent that will open the browser to the beacon's url
    // if the user taps the notification
    remoteViews.setOnClickPendingIntent(R.id.first_beacon_main_layout,
            Utils.createNavigateToUrlPendingIntent(pwsResult, this));
    remoteViews.setViewVisibility(R.id.firstBeaconLayout, View.VISIBLE);
}

From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.UrlDeviceDiscoveryService.java

private void updateSummaryNotificationRemoteViewsSecondBeacon(PwPair pwPair, RemoteViews remoteViews) {
    PwsResult pwsResult = pwPair.getPwsResult();
    remoteViews.setImageViewBitmap(R.id.icon_secondBeacon, Utils.getBitmapIcon(mPwCollection, pwsResult));
    remoteViews.setTextViewText(R.id.title_secondBeacon, pwsResult.getTitle());
    remoteViews.setTextViewText(R.id.url_secondBeacon, pwsResult.getSiteUrl());
    remoteViews.setTextViewText(R.id.description_secondBeacon, pwsResult.getDescription());
    // Recolor notifications to have light text for non-Lollipop devices
    if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
        remoteViews.setTextColor(R.id.title_secondBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR);
        remoteViews.setTextColor(R.id.url_secondBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR);
        remoteViews.setTextColor(R.id.description_secondBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR);
    }//w  w w.  j a  va2  s .c om

    // Create an intent that will open the browser to the beacon's url
    // if the user taps the notification
    remoteViews.setOnClickPendingIntent(R.id.second_beacon_main_layout,
            Utils.createNavigateToUrlPendingIntent(pwsResult, this));
    remoteViews.setViewVisibility(R.id.secondBeaconLayout, View.VISIBLE);
}