Lorenzo1818

CryptoWidget

Jan 19th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package com.lorenzobenevento.cryptowidget;
  2.  
  3. import android.appwidget.AppWidgetManager;
  4. import android.appwidget.AppWidgetProvider;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.widget.RemoteViews;
  8.  
  9. import java.text.DateFormat;
  10. import java.util.Date;
  11.  
  12. import static android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE;
  13.  
  14. /**
  15.  * Implementation of App Widget functionality.
  16.  */
  17. public class CryptoWidget extends AppWidgetProvider {
  18.  
  19.     static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
  20.                                 int appWidgetId) {
  21.  
  22.         CharSequence widgetText = context.getString(R.string.appwidget_text);
  23.  
  24.         String currentDataTimeString = DateFormat.getDateTimeInstance().format(new Date());
  25.         // Construct the RemoteViews object
  26.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.crypto_widget);
  27.         views.setTextViewText(R.id.cryptowidget, currentDataTimeString);
  28.  
  29.         // Instruct the widget manager to update the widget
  30.         appWidgetManager.updateAppWidget(appWidgetId, views);
  31.     }
  32.  
  33.     @Override
  34.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  35.         // There may be multiple widgets active, so update all of them
  36.  
  37.         for (int appWidgetId : appWidgetIds) {
  38.             updateAppWidget(context, appWidgetManager, appWidgetId);
  39.         }
  40.     }
  41.  
  42.     @Override
  43.     public void onEnabled(Context context) {
  44.         // Enter relevant functionality for when the first widget is created
  45.     }
  46.  
  47.     @Override
  48.     public void onDisabled(Context context) {
  49.         // Enter relevant functionality for when the last widget is disabled
  50.     }
  51.  
  52.     public static void sendUpdateIntent(Context context) {
  53.         Intent intent = new Intent(context, CryptoWidget.class);
  54.         intent.setAction(ACTION_APPWIDGET_UPDATE);
  55.         context.sendBroadcast(intent);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment