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 handleIsOpen(final Context mContext, final OpeningStatus openingStatus) {
    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    final int[] appWidgetIds = appWidgetManager
            .getAppWidgetIds(new ComponentName(mContext, AnalogWidget.class));
    final RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.analog_widget);
    views.setTextViewText(R.id.appwidget_text, mContext.getText(R.string.refreshing_analog));
    views.setTextColor(R.id.appwidget_text,
            ContextCompat.getColor(mContext, android.R.color.primary_text_dark));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }/*from www  . ja va2  s  .co  m*/

    CharSequence widgetText;
    if (openingStatus.open) {
        widgetText = mContext.getString(R.string.widget_open_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.openColor));
    } else {
        widgetText = mContext.getString(R.string.widget_closed_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.closedColor));
    }

    views.setTextViewText(R.id.appwidget_text, widgetText);
    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 source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java

/**
 * Create the big view for the summary notification
 *//*from   w w  w . j a  v  a 2 s. c om*/
private RemoteViews updateSummaryNotificationRemoteViews() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_big_view);

    // Fill in the data for the top two beacon views
    updateSummaryNotificationRemoteViewsFirstBeacon(mDeviceAddressToUrl.get(mSortedDevices.get(0)),
            remoteViews);
    updateSummaryNotificationRemoteViewsSecondBeacon(mDeviceAddressToUrl.get(mSortedDevices.get(1)),
            remoteViews);

    // Create a pending intent that will open the physical web app
    // TODO: Use a clickListener on the VIEW MORE button to do this
    PendingIntent pendingIntent = createReturnToAppPendingIntent();
    remoteViews.setOnClickPendingIntent(R.id.otherBeaconsLayout, pendingIntent);

    return remoteViews;
}

From source file:de.micmun.android.workdaystarget.DaysLeftService.java

/**
 * Updates the days to target.//from w  ww .  java  2 s .c  o m
 */
private void updateDays() {
    AppWidgetManager appManager = AppWidgetManager.getInstance(this);
    ComponentName cName = new ComponentName(getApplicationContext(), DaysLeftProvider.class);
    int[] appIds = appManager.getAppWidgetIds(cName);

    DayCalculator dayCalc = new DayCalculator();
    if (!isOnline()) {
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            Log.e(TAG, "Interrupted: " + e.getLocalizedMessage());
        }
    }

    for (int appId : appIds) {
        PrefManager pm = new PrefManager(this, appId);
        Calendar target = pm.getTarget();
        boolean[] chkDays = pm.getCheckedDays();
        int days = pm.getLastDiff();

        if (isOnline()) {
            try {
                days = dayCalc.getDaysLeft(target.getTime(), chkDays);
                Map<String, Object> saveMap = new HashMap<String, Object>();
                Long diff = Long.valueOf(days);
                saveMap.put(PrefManager.KEY_DIFF, diff);
                pm.save(saveMap);
            } catch (JSONException e) {
                Log.e(TAG, "ERROR holidays: " + e.getLocalizedMessage());
            }
        } else {
            Log.e(TAG, "No internet connection!");
        }
        RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.appwidget_layout);
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        String targetStr = df.format(target.getTime());
        rv.setTextViewText(R.id.target, targetStr);
        String dayStr = String.format(Locale.getDefault(), "%d %s", days,
                getResources().getString(R.string.unit));
        rv.setTextViewText(R.id.dayCount, dayStr);

        // put widget id into intent
        Intent configIntent = new Intent(this, ConfigActivity.class);
        configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        configIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appId);
        configIntent.setData(Uri.parse(configIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendIntent = PendingIntent.getActivity(this, 0, configIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        rv.setOnClickPendingIntent(R.id.widgetLayout, pendIntent);

        // update widget
        appManager.updateAppWidget(appId, rv);
    }
}

From source file:com.abhijitvalluri.android.fitnotifications.widget.ServiceToggle.java

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (TOGGLE_CLICKED.equals(intent.getAction())) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.service_toggle_widget);
        if (NLService.isEnabled()) {
            NLService.setEnabled(false);
            views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_off_white_48dp);
            views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_red);
            views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_off_text));
            views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.red));
        } else {/*from w ww  . j a  v a 2  s . c  o m*/
            NLService.setEnabled(true);
            views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_white_48dp);
            views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_green);
            views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_on_text));
            views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.green));
        }

        views.setOnClickPendingIntent(R.id.widgetToggleButton,
                getPendingSelfIntent(context, 0, TOGGLE_CLICKED));

        ComponentName componentName = new ComponentName(context, ServiceToggle.class);
        appWidgetManager.updateAppWidget(componentName, views);
    }
}

From source file:ru.org.sevn.audiobookplayer.AppService.java

private NotificationCompat.Builder makeNotify(Context ctx, String title, String text) {
    //Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
    NotificationCompat.Builder mBuilder = makeNotifyBuilder(ctx, title, text);

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
    notificationView.setTextViewText(R.id.tvTitle, title);
    notificationView.setTextViewText(R.id.textView1, "" + makeNotifyNumber() + " " + text);
    notificationView.setImageViewBitmap(R.id.imageView1, getLargeNotificationIcon());
    mBuilder.setContent(notificationView);
    mBuilder.setContentIntent(makePendingNotificationIntent(makeNotificationIntent()));

    //       notification.contentView = notificationView;
    //       notification.contentIntent = makePendingNotificationIntent(makeNotificationIntent());
    //       notification.flags |= Notification.FLAG_NO_CLEAR;

    notificationView.setOnClickPendingIntent(R.id.btn_prev, makeActionPendingIntent(ACTION_PREV));
    notificationView.setOnClickPendingIntent(R.id.btn_play, makeActionPendingIntent(ACTION_PLAY_PAUSE));
    notificationView.setOnClickPendingIntent(R.id.btn_next, makeActionPendingIntent(ACTION_NEXT));
    notificationView.setOnClickPendingIntent(R.id.btn_exit, makeActionPendingIntent(ACTION_EXIT));

    return mBuilder;
}

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

/**
 * This method builds the base, otherwise known as the collapsed notification. The expanded
 * notification method builds upon this method.
 *
 * @param resultView The RemoteView to begin with, be it new or from the current notification.
 * @return The base, otherwise known as, collapsed notification resources for RemoteViews.
 *///from  www.  j a v a  2 s.  com
private RemoteViews buildBaseNotification(final RemoteViews resultView) {
    final String title = mCurrentMusic.getTitle();

    /** If in streaming, the notification should be persistent. */
    if (app.getApplicationState().streamingMode && !StreamingService.isWoundDown()) {
        resultView.setViewVisibility(R.id.notificationClose, View.GONE);
    } else {
        resultView.setViewVisibility(R.id.notificationClose, View.VISIBLE);
        resultView.setOnClickPendingIntent(R.id.notificationClose, notificationClose);
    }

    if (MPDStatus.MPD_STATE_PLAYING.equals(getStatus().getState())) {
        resultView.setOnClickPendingIntent(R.id.notificationPlayPause, notificationPause);
        resultView.setImageViewResource(R.id.notificationPlayPause, R.drawable.ic_media_pause);
    } else {
        resultView.setOnClickPendingIntent(R.id.notificationPlayPause, notificationPlay);
        resultView.setImageViewResource(R.id.notificationPlayPause, R.drawable.ic_media_play);
    }

    /** When streaming, move things down (hopefully, very) temporarily. */
    if (mediaPlayerServiceIsBuffering) {
        resultView.setTextViewText(R.id.notificationTitle, getString(R.string.buffering));
        resultView.setTextViewText(R.id.notificationArtist, title);
    } else {
        resultView.setTextViewText(R.id.notificationTitle, title);
        resultView.setTextViewText(R.id.notificationArtist, mCurrentMusic.getArtist());
    }

    resultView.setOnClickPendingIntent(R.id.notificationNext, notificationNext);

    if (mAlbumCover != null) {
        resultView.setImageViewUri(R.id.notificationIcon, Uri.parse(mAlbumCoverPath));
    }

    return resultView;
}

From source file:com.lithiumli.fiction.PlaybackService.java

private void showNotification() {
    Intent launchPlaybackIntent = new Intent(getApplicationContext(), NowPlayingActivity.class);
    launchPlaybackIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, launchPlaybackIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    String title = "(unknown title)";
    String artist = "(unknown artist)";
    String album = "(unknown album)";
    Uri albumArt = Song.DEFAULT_ALBUM;//from  www  . java2s  . c om
    if (mQueue.getCount() != 0) {
        Song song = mQueue.getCurrent();
        title = song.getTitle();
        artist = song.getArtist();
        album = song.getAlbum();
        albumArt = song.getAlbumArt();

        // TODO see ImageView.resolveUri for a working method
        // if (albumArt.getPath() == null) {
        //     albumArt = Song.DEFAULT_ALBUM;
        // }
    }

    Notification.Builder builder = new Notification.Builder(getApplicationContext());
    builder.setSmallIcon(R.drawable.ic_menu_play).setContentTitle("Playing" + title).setOngoing(true)
            .setContentIntent(pi);
    Notification notification = builder.build();

    RemoteViews customView = new RemoteViews(getPackageName(), R.layout.notification);
    customView.setImageViewUri(R.id.notification_cover, albumArt);
    customView.setTextViewText(R.id.notification_title, title);
    customView.setTextViewText(R.id.notification_subtitle, artist);
    notification.contentView = customView;

    customView = new RemoteViews(getPackageName(), R.layout.notification_big);
    customView.setImageViewUri(R.id.notification_cover, albumArt);
    customView.setImageViewResource(R.id.notification_play_pause,
            mPaused ? R.drawable.ic_menu_play : R.drawable.ic_menu_pause);
    customView.setTextViewText(R.id.notification_title, title);
    customView.setTextViewText(R.id.notification_album, album);
    customView.setTextViewText(R.id.notification_artist, artist);
    customView.setOnClickPendingIntent(R.id.notification_previous, createAction(ACTION_PREV));
    customView.setOnClickPendingIntent(R.id.notification_play_pause, createAction(ACTION_PLAY_PAUSE));
    customView.setOnClickPendingIntent(R.id.notification_next, createAction(ACTION_NEXT));
    notification.bigContentView = customView;

    startForeground(NOTIFICATION_PLAYING, notification);
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_PLAYING,
            notification);
}

From source file:com.tct.mail.widget.WidgetService.java

public static void configureValidWidgetIntents(Context context, RemoteViews remoteViews, int appWidgetId,
        Account account, final int folderType, final int folderCapabilities, final Uri folderUri,
        final Uri folderConversationListUri, final String folderDisplayName, Class<?> serviceClass) {
    remoteViews.setViewVisibility(R.id.widget_configuration, View.GONE);

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, serviceClass);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_TYPE, folderType);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_CAPABILITIES, folderCapabilities);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_URI, folderUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_CONVERSATION_LIST_URI, folderConversationListUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_DISPLAY_NAME, folderDisplayName);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(R.id.conversation_list, intent);
    remoteViews.setEmptyView(R.id.conversation_list, R.id.empty_conversation_list); //TS: zheng.zou 2015-08-11 EMAIL BUGFIX_1044483 ADD
    // Open mail app when click on header
    final Intent mailIntent = Utils.createViewFolderIntent(context, folderUri, account);
    PendingIntent clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent();
    composeIntent.setAction(Intent.ACTION_SEND);
    //TS: jian.xu 2015-4-24 EMAIL BUGFIX_984619 ADD_S
    composeIntent.setPackage(context.getPackageName());
    //TS: jian.xu 2015-4-24 EMAIL BUGFIX_984619 ADD_E
    composeIntent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    composeIntent.setData(account.composeIntentUri);
    composeIntent.putExtra(ComposeActivity.EXTRA_FROM_EMAIL_TASK, true);
    composeIntent.putExtra(ComposeActivity.EXTRA_FROM_EMAIL_WIDGET, true); //TS: zheng.zou 2015-12-09 EMAIL BUGFIX_1059178 ADD
    if (account.composeIntentUri != null) {
        composeIntent.putExtra(Utils.EXTRA_COMPOSE_URI, account.composeIntentUri);
    }/*  w  ww.j ava2 s.c o m*/

    // Build a task stack that forces the conversation list on the stack before the compose
    // activity.
    final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    clickIntent = taskStackBuilder.addNextIntent(mailIntent).addNextIntent(composeIntent).getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    final Intent conversationIntent = new Intent();
    conversationIntent.setAction(Intent.ACTION_VIEW);
    clickIntent = PendingIntent.getActivity(context, 0, conversationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list, clickIntent);
}

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 {/*from w  w  w .j a va  2s .c  o  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);
}

From source file:com.nd.android.u.square.service.MusicPlaybackService.java

Notification buildNotification(String text) {
    //        Log.d("yulin", "buildNotification, musicName = " + musicName + ", mIsPause = " + mIsPause);

    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);
    mNotificationBuilder.setOngoing(true);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setSmallIcon(R.drawable.ic_square_notification_music_play);
    mNotificationBuilder.setTicker(text);

    //Grab the notification layout.
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.square_notification_music_play);

    Intent stopServiceIntent = new Intent(ACTION_STOP);
    PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
            stopServiceIntent, 0);/*from   w w  w  .j  a  va2  s .  co m*/

    //Set the notification content.

    notificationView.setTextViewText(R.id.notification_base_line_one, text);

    //Set the "Stop Service" pending intent.
    notificationView.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);

    //Set the album art.
    notificationView.setImageViewResource(R.id.notification_base_image, R.drawable.ic_square_music_default);

    //Attach the shrunken layout to the notification.
    mNotificationBuilder.setContent(notificationView);

    //Build the notification object and set its flags.
    Notification notification = mNotificationBuilder.build();
    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
            | Notification.FLAG_ONGOING_EVENT;

    return notification;
}