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.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId/*from  www .  jav a  2  s.c o m*/
 */
private static void updateWidget(Context context, int appWidgetId) {
    if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
        Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
    }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    PendingIntent clickIntent;

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, MmsWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);

    remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.sms_app_label));

    // Open Mms's app conversation list when click on header
    final Intent convIntent = new Intent(context, ConversationList.class);
    clickIntent = PendingIntent.getActivity(context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
    composeIntent.setAction(Intent.ACTION_SENDTO);
    clickIntent = PendingIntent.getActivity(context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ComposeMessageActivity.class);
    Intent msgIntent = new Intent(Intent.ACTION_VIEW);
    msgIntent.setType("vnd.android-dir/mms-sms");
    taskStackBuilder.addNextIntent(msgIntent);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list,
            taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
}

From source file:net.sourceforge.kalimbaradio.androidapp.util.NotificationUtil.java

private static Notification createCustomNotification(Context context, MusicDirectory.Entry song,
        boolean playing) {

    Bitmap albumArt;/*from   w w w . j a v  a  2 s.c  o m*/
    try {
        albumArt = FileUtil.getUnscaledAlbumArtBitmap(context, song);
        if (albumArt == null) {
            albumArt = Util.decodeBitmap(context, R.drawable.unknown_album_large);
        }
    } catch (Exception x) {
        LOG.warn("Failed to get notification cover art", x);
        albumArt = Util.decodeBitmap(context, R.drawable.unknown_album_large);
    }

    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
    contentView.setTextViewText(R.id.notification_title, song.getTitle());
    contentView.setTextViewText(R.id.notification_artist, song.getArtist());
    contentView.setImageViewBitmap(R.id.notification_image, albumArt);
    contentView.setImageViewResource(R.id.notification_playpause,
            playing ? R.drawable.media_pause : R.drawable.media_start);

    Intent intent = new Intent("1");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    contentView.setOnClickPendingIntent(R.id.notification_playpause,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("2");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    contentView.setOnClickPendingIntent(R.id.notification_next,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("4");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Constants.INTENT_EXTRA_NAME_HIDE_NOTIFICATION, true);
    contentView.setOnClickPendingIntent(R.id.notification_close,
            PendingIntent.getService(context, 0, intent, 0));

    Intent notificationIntent = new Intent(context, DownloadActivity.class);

    Notification notification = new NotificationCompat.Builder(context).setOngoing(true)
            .setSmallIcon(R.drawable.stat_notify_playing).setContent(contentView)
            .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0)).build();
    if (Build.VERSION.SDK_INT >= 16) {
        notification.bigContentView = createBigContentView(context, song, albumArt, playing);
    }
    return notification;
}

From source file:android.support.v7.app.NotificationCompatImplBase.java

private static <T extends NotificationCompatBase.Action> RemoteViews generateBigContentView(Context context,
        CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, int number,
        Bitmap largeIcon, CharSequence subText, boolean useChronometer, long when, List<T> actions,
        boolean showCancelButton, PendingIntent cancelButtonIntent) {
    final int actionCount = Math.min(actions.size(), MAX_MEDIA_BUTTONS);
    RemoteViews big = applyStandardTemplate(context, contentTitle, contentText, contentInfo, number, largeIcon,
            subText, useChronometer, when, getBigLayoutResource(actionCount), false /* fitIn1U */);

    big.removeAllViews(R.id.media_actions);
    if (actionCount > 0) {
        for (int i = 0; i < actionCount; i++) {
            final RemoteViews button = generateMediaActionButton(context, actions.get(i));
            big.addView(R.id.media_actions, button);
        }/*from ww  w  .j  av  a 2 s  . c o  m*/
    }
    if (showCancelButton) {
        big.setViewVisibility(R.id.cancel_action, View.VISIBLE);
        big.setInt(R.id.cancel_action, "setAlpha",
                context.getResources().getInteger(R.integer.cancel_button_image_alpha));
        big.setOnClickPendingIntent(R.id.cancel_action, cancelButtonIntent);
    } else {
        big.setViewVisibility(R.id.cancel_action, View.GONE);
    }
    return big;
}

From source file:org.cowboycoders.cyclisimo.widgets.TrackWidgetProvider.java

/**
 * Updates the statistics container.// w  w  w.ja  v a  2 s .c  o m
 * 
 * @param context the context
 * @param remoteViews the remote views
 * @param track the track
 */
private static void updateStatisticsContainer(Context context, RemoteViews remoteViews, Track track) {
    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.create(context);
    taskStackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, 0);
    remoteViews.setOnClickPendingIntent(R.id.track_widget_stats_container, pendingIntent);
}

From source file:org.cowboycoders.cyclisimo.widgets.TrackWidgetProvider.java

/**
 * Updates the stop button.// w w  w  .j av a2 s .  co m
 * 
 * @param context the context
 * @param remoteViews the remote views
 * @param isRecording true if recording
 */
private static void updateStopButton(Context context, RemoteViews remoteViews, boolean isRecording) {
    remoteViews.setImageViewResource(R.id.track_widget_stop_button,
            isRecording ? R.drawable.btn_stop_1 : R.drawable.btn_stop_0);
    remoteViews.setBoolean(R.id.track_widget_stop_button, "setEnabled", isRecording);
    if (isRecording) {
        Intent intent = new Intent(context, ControlRecordingService.class)
                .setAction(context.getString(R.string.track_action_end));
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.track_widget_stop_button, pendingIntent);
    }
}

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

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

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

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

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

    final String packageName = context.getPackageName();

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

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

    final Notification notification = builder.build();

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

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

From source file:net.sourceforge.kalimbaradio.androidapp.util.NotificationUtil.java

private static RemoteViews createBigContentView(Context context, MusicDirectory.Entry song, Bitmap albumArt,
        boolean playing) {

    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
    contentView.setTextViewText(R.id.notification_title, song.getTitle());
    contentView.setTextViewText(R.id.notification_artist, song.getArtist());
    contentView.setTextViewText(R.id.notification_album, song.getAlbum());
    contentView.setImageViewBitmap(R.id.notification_image, albumArt);
    contentView.setImageViewResource(R.id.notification_playpause,
            playing ? R.drawable.media_pause : R.drawable.media_start);

    Intent intent = new Intent("1");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    contentView.setOnClickPendingIntent(R.id.notification_playpause,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("2");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    contentView.setOnClickPendingIntent(R.id.notification_next,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("3");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
    contentView.setOnClickPendingIntent(R.id.notification_prev,
            PendingIntent.getService(context, 0, intent, 0));

    intent = new Intent("4");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Constants.INTENT_EXTRA_NAME_HIDE_NOTIFICATION, true);
    contentView.setOnClickPendingIntent(R.id.notification_close,
            PendingIntent.getService(context, 0, intent, 0));

    return contentView;
}

From source file:br.com.bioscada.apps.biotracks.widgets.TrackWidgetProvider.java

/**
 * Updates the stop button./*  w w  w  .ja va  2s . c  om*/
 * 
 * @param context the context
 * @param remoteViews the remote views
 * @param isRecording true if recording
 */
private static void updateStopButton(Context context, RemoteViews remoteViews, boolean isRecording) {
    remoteViews.setImageViewResource(R.id.track_widget_stop_button,
            isRecording ? R.drawable.button_stop : R.drawable.ic_button_stop_disabled);
    remoteViews.setBoolean(R.id.track_widget_stop_button, "setEnabled", isRecording);
    if (isRecording) {
        Intent intent = new Intent(context, ControlRecordingService.class)
                .setAction(context.getString(R.string.track_action_end));
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.track_widget_stop_button, pendingIntent);
    }
}

From source file:br.com.bioscada.apps.biotracks.widgets.TrackWidgetProvider.java

/**
 * Updates the statistics container.//  ww  w  .j  a  va2  s .co  m
 * 
 * @param context the context
 * @param remoteViews the remote views
 * @param track the track
 */
private static void updateStatisticsContainer(Context context, RemoteViews remoteViews, Track track) {
    PendingIntent pendingIntent;
    if (track != null) {
        Intent intent = IntentUtils.newIntent(context, TrackDetailActivity.class)
                .putExtra(TrackDetailActivity.EXTRA_TRACK_ID, track.getId());
        pendingIntent = TaskStackBuilder.create(context).addParentStack(TrackDetailActivity.class)
                .addNextIntent(intent).getPendingIntent(0, 0);
    } else {
        Intent intent = IntentUtils.newIntent(context, TrackListActivity.class);
        pendingIntent = TaskStackBuilder.create(context).addNextIntent(intent).getPendingIntent(0, 0);
    }
    remoteViews.setOnClickPendingIntent(R.id.track_widget_stats_container, pendingIntent);
}

From source file:org.openbitcoinwidget.WidgetProvider.java

private static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    WidgetPreferences preferences = PreferencesActivity.getWidgetPreferences(context, appWidgetId);

    if (preferences == null) {
        // Don't do anything unless the rate service has been chosen.
        // Show a "please remove this widget and add a new one"
        appWidgetManager.updateAppWidget(appWidgetId,
                new RemoteViews(context.getPackageName(), R.layout.appwidget_replace_me));
        return;//w  w  w  . j  a  va2  s. c o  m
    }

    boolean isOnLockScreen = isWidgetShownOnLockScreen(appWidgetManager, appWidgetId);

    RemoteViews views;
    if (isOnLockScreen) {
        views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_lock_screen);
    } else {
        views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
    }

    Intent clickIntent = new Intent(context, GraphPopupActivity.class);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    clickIntent.setAction("dummyAction"); // Needed to get the extra variables included in the call
    // Note: the appWidgetId needs to be sent in the pendingIntent as request code, otherwise only ONE
    //      cached intent will be used for all widget instances!
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, clickIntent, 0);
    views.setOnClickPendingIntent(R.id.appwidget_box, pendingIntent);
    views.setTextViewText(R.id.appwidget_service_name, preferences.getRateService().getName());

    DataOpenHelper dbHelper = new DataOpenHelper(context);
    TickerData prevData = dbHelper.getLastTickerData(preferences);

    TickerData newData;
    String latestQuote = getLatestQuote(preferences);
    if (latestQuote != null && !latestQuote.equals("")) {
        newData = preferences.getRateService().parseJSON(latestQuote);
        newData.setCurrencyConversion(preferences.getCurrencyConversion());
        storeLastValueIfNotNull(dbHelper, newData);
        updateViews(views, prevData, newData, preferences);
    } else if (prevData != null) {
        newData = prevData;
        updateViews(views, prevData, newData, preferences);
    } else {
        updateViewsWithError(views, preferences);
    }
    appWidgetManager.updateAppWidget(appWidgetId, views);
}