Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lorenzobenevento.cryptowidget;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.Context;
- import android.content.Intent;
- import android.widget.RemoteViews;
- import java.text.DateFormat;
- import java.util.Date;
- import static android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE;
- /**
- * Implementation of App Widget functionality.
- */
- public class CryptoWidget extends AppWidgetProvider {
- static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
- int appWidgetId) {
- CharSequence widgetText = context.getString(R.string.appwidget_text);
- String currentDataTimeString = DateFormat.getDateTimeInstance().format(new Date());
- // Construct the RemoteViews object
- RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.crypto_widget);
- views.setTextViewText(R.id.cryptowidget, currentDataTimeString);
- // Instruct the widget manager to update the widget
- appWidgetManager.updateAppWidget(appWidgetId, views);
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
- // There may be multiple widgets active, so update all of them
- for (int appWidgetId : appWidgetIds) {
- updateAppWidget(context, appWidgetManager, appWidgetId);
- }
- }
- @Override
- public void onEnabled(Context context) {
- // Enter relevant functionality for when the first widget is created
- }
- @Override
- public void onDisabled(Context context) {
- // Enter relevant functionality for when the last widget is disabled
- }
- public static void sendUpdateIntent(Context context) {
- Intent intent = new Intent(context, CryptoWidget.class);
- intent.setAction(ACTION_APPWIDGET_UPDATE);
- context.sendBroadcast(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment