Example usage for android.widget RemoteViews setPendingIntentTemplate

List of usage examples for android.widget RemoteViews setPendingIntentTemplate

Introduction

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

Prototype

public void setPendingIntentTemplate(int viewId, PendingIntent pendingIntentTemplate) 

Source Link

Document

When using collections (eg.

Usage

From source file:com.udacity.stockhawk.ui.widget.StockWidget.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int widgetId : appWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_stock);

        Intent clickIntentTemplate = new Intent(context, StockChart.class);

        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.lv_widget_stock, clickPendingIntentTemplate);

        // Set the collection and update the widget
        setRemoteAdapter(context, remoteViews);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    }/*from w w  w. j  a  v a  2s  .  co  m*/

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

From source file:com.udacity.stockhawk.widget.StockAppWidgetProvider.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.stock_app_widget);

        // Set up the collection
        views.setRemoteAdapter(R.id.lv_stock_list, new Intent(context, StockAppRemoteViewsService.class));

        Intent clickIntentTemplate = new Intent(context, ChartActivity.class);
        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntentTemplate)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.lv_stock_list, clickPendingIntentTemplate);

        views.setEmptyView(R.id.lv_stock_list, R.id.tv_widget_empty);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }/* w  w w  .  jav  a 2  s. c o  m*/
}

From source file:com.concentriclivers.mms.com.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId/*from  w w  w.j av a2 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.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:com.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId//from w  w w .  j  av 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: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 {// ww w.  j a v a2 s .  co  m
            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.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);/*www .j  a v a  2  s. co 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:co.carlosjimenez.android.currencyalerts.app.widget.DetailWidgetProvider.java

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

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

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

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

        // Set up the collection
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setRemoteAdapter(context, views, appWidgetIds);
        } else {/*www.j a  va2 s.  c  o  m*/
            setRemoteAdapterV11(context, views, appWidgetIds);
        }
        Intent clickIntentTemplate = new Intent(context, DetailActivity.class);

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

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

From source file:com.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 {/*from  ww w  .j  a v 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.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 ww. j  a  v  a2 s  . c o 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:com.ravi.apps.android.newsbytes.widget.NewsWidgetProvider.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d(LOG_TAG, context.getString(R.string.log_on_update));

    // Update each app widget instance with remote view and corresponding remote adapter.
    for (int appWidgetId : appWidgetIds) {
        // Create the remote views object for the widget instance.
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_news);

        // Create the intent that starts the service which will provide the views for this collection.
        Intent serviceIntent = new Intent(context, NewsWidgetRemoteViewsService.class);

        // Add the app widget id to the intent extras.
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));

        // Set the remote adapter onto the scores list view in the remote view.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            remoteView.setRemoteAdapter(R.id.widget_list_view, serviceIntent);
        } else {/*  w w w  .j  a v a  2 s  . co m*/
            remoteView.setRemoteAdapter(appWidgetId, R.id.widget_list_view, serviceIntent);
        }

        // Create an intent to launch the main activity and set it on the remote view's title.
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        remoteView.setOnClickPendingIntent(R.id.widget, pendingIntent);

        // Create and add the list item click pending intent template.
        Intent listItemClickIntent = new Intent(context, MainActivity.class);
        listItemClickIntent.setAction(context.getString(R.string.action_item_clicked))
                .setData(Uri.parse(listItemClickIntent.toUri(Intent.URI_INTENT_SCHEME)));

        // Set the pending intent template.
        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(listItemClickIntent)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteView.setPendingIntentTemplate(R.id.widget_list_view, clickPendingIntentTemplate);

        // Set an empty view in case of no data.
        remoteView.setEmptyView(R.id.widget_list_view, R.id.widget_empty);

        // Call app widget manager to update the app widget instance.
        appWidgetManager.updateAppWidget(appWidgetId, remoteView);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}