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:dk.cafeanalog.AnalogWidget.java

private void handleError(Context mContext) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.analog_widget);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(mContext, AnalogWidget.class));
    views.setTextViewText(R.id.appwidget_text, "Error");
    views.setOnClickPendingIntent(R.id.appwidget_text, getPendingSelfIntent(mContext));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }//from   w w  w. j a  v a  2  s. c  o  m
}

From source file:com.securecomcode.text.service.KeyCachingService.java

private void foregroundServiceICS() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.key_caching_notification);

    remoteViews.setOnClickPendingIntent(R.id.lock_cache_icon, buildLockIntent());

    builder.setSmallIcon(R.drawable.icon_cached);
    builder.setContent(remoteViews);//www . j a v a2 s  .  co  m
    builder.setContentIntent(buildLaunchIntent());

    stopForeground(true);
    startForeground(SERVICE_RUNNING_ID, builder.build());
}

From source file:org.jitsi.android.gui.call.notification.CallNotificationManager.java

/**
 * Binds pending intents to all control <tt>Views</tt>.
 * @param ctx Android context.//  ww w .  j ava  2s . co m
 * @param contentView notification root <tt>View</tt>.
 * @param callID the call ID that will be used in the <tt>Intents</tt>
 */
private void setIntents(Context ctx, RemoteViews contentView, String callID) {
    // Hangup button
    PendingIntent pHangup = PendingIntent.getBroadcast(ctx, 0, CallControl.getHangupIntent(callID),
            PendingIntent.FLAG_CANCEL_CURRENT);
    contentView.setOnClickPendingIntent(R.id.hangup_button, pHangup);

    // Speakerphone button
    PendingIntent pSpeaker = PendingIntent.getBroadcast(ctx, 1, CallControl.getToggleSpeakerIntent(callID),
            PendingIntent.FLAG_CANCEL_CURRENT);
    contentView.setOnClickPendingIntent(R.id.speakerphone, pSpeaker);

    // Mute button
    PendingIntent pMute = PendingIntent.getBroadcast(ctx, 2, CallControl.getToggleMuteIntent(callID),
            PendingIntent.FLAG_CANCEL_CURRENT);
    contentView.setOnClickPendingIntent(R.id.mute_button, pMute);

    // Hold button
    PendingIntent pHold = PendingIntent.getBroadcast(ctx, 3, CallControl.getToggleOnHoldIntent(callID),
            PendingIntent.FLAG_CANCEL_CURRENT);
    contentView.setOnClickPendingIntent(R.id.hold_button, pHold);

    // Show video call Activity
    Intent videoCall = new Intent(ctx, VideoCallActivity.class);
    videoCall.putExtra(CallManager.CALL_IDENTIFIER, callID);
    PendingIntent pVideo = PendingIntent.getActivity(ctx, 4, videoCall, PendingIntent.FLAG_CANCEL_CURRENT);
    contentView.setOnClickPendingIntent(R.id.back_to_call, pVideo);

    // Binds show video call intent to the whole area
    contentView.setOnClickPendingIntent(R.id.notificationContent, pVideo);
}

From source file:com.xengar.android.stocktracker.widget.ListWidgetProvider.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_list);

        // 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 collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views);
        } else {/*w ww.  jav  a 2  s. c  o m*/
            setRemoteAdapterV11(context, views);
        }
        boolean useDetailActivity = context.getResources().getBoolean(R.bool.use_detail_activity);
        Intent clickIntentTemplate = useDetailActivity ? new Intent(context, DetailActivity.class)
                : new Intent(context, MainActivity.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.chen.mail.utils.NotificationActionUtils.java

public static Notification createUndoNotification(final Context context,
        final NotificationAction notificationAction, final int notificationId) {
    LogUtils.i(LOG_TAG, "createUndoNotification for %s", notificationAction.getNotificationActionType());

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    builder.setSmallIcon(R.drawable.stat_notify_email);
    builder.setWhen(notificationAction.getWhen());

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

    final String packageName = context.getPackageName();

    final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO);
    clickIntent.setPackage(packageName);
    putNotificationActionExtra(clickIntent, notificationAction);
    final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId, clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent);

    builder.setContent(undoView);/*  ww w . j a va2 s  .co  m*/

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

    final Notification notification = builder.build();

    return notification;
}

From source file:com.example.olivi.maphap.widget.DetailWidgetProvider.java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    //Start up DetailWidgetIntentService to query for regions data and see when it was last
    // updated. If the data is too old, the service will update it using MapHapService.
    Intent i = new Intent(context, DetailWidgetIntentService.class);
    context.startService(i);/*from   w  w  w .j a  v a2  s .c  o m*/

    // 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_detail, pendingIntent);

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views);
        } else {
            setRemoteAdapterV11(context, views);
        }

        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 com.example
        // .olivi.maphap.widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

From source file:barqsoft.footballscores.widget.WidgetListProvider.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_list);

        // 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_item, pendingIntent);

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views);
        } else {/*from www.  j  a v  a 2  s .c  om*/
            setRemoteAdapterV11(context, views);
        }
        Intent clickIntentTemplate = new Intent(context, MainActivity.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.android.deskclock.data.StopwatchNotificationBuilderPreN.java

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes//  w w w .j av a  2 s . co  m
    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;
}

From source file:com.b44t.messenger.MusicPlayerService.java

public void setListeners(RemoteViews view) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent(NOTIFY_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_close, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_next, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
}

From source file:com.ferdi2005.secondgram.MusicPlayerService.java

public void setListeners(RemoteViews view) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent(NOTIFY_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_close, pendingIntent);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_next, pendingIntent);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
}