下面列出了android.appwidget.AppWidgetManager#updateAppWidget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget_profile);
widget.setRemoteAdapter(R.id.profile_list, svcIntent);
widget.setPendingIntentTemplate(R.id.profile_list, getPendingIntent(context, LIST_ITEM_CLICK));
appWidgetManager.updateAppWidget(appWidgetId, widget);
}
}
@Override public void onUpdate(Context cntx, AppWidgetManager manager,
int[] widgetIds) {
final int l = widgetIds.length;
for (int i = 0; i != l; ++i) {
int widgetId = widgetIds[i];
Log.d("Budget", "WidgetProvider.id="+widgetId);
RemoteViews views = new RemoteViews(
cntx.getPackageName(),
R.layout.widget
);
Intent srv = new Intent(cntx, WidgetService.class);
srv.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetIds[i]);
srv.setData(Uri.parse(srv.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(widgetIds[i], R.id.grid, srv);
views.setEmptyView(R.id.grid, R.id.empty);
Intent act = new Intent(cntx, EnvelopesActivity.class);
act.setData(Uri.parse("fragment://"+EnvelopeDetailsFragment.class.getName()+"/"+widgetIds[i]));
act.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetIds[i]);
PendingIntent actPending = PendingIntent.getActivity(
cntx, 0, act, PendingIntent.FLAG_UPDATE_CURRENT
);
views.setPendingIntentTemplate(R.id.grid, actPending);
manager.updateAppWidget(widgetIds[i], views);
}
super.onUpdate(cntx, manager, widgetIds);
}
@Override
public void onCreate() {
super.onCreate();
Utils.logInfo(ToggleService.class.getSimpleName(), "Started toggle service");
Prefs prefs = new Prefs(getApplicationContext());
prefs.apply();
starterServiceIntent = new Intent(getApplicationContext(), StarterService.class);
prefs.setBool(Prefs.KEYS.ENABLED.toString(), !prefs.enabled);
hideNotification();
restartService();
Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
remoteViews.setTextColor(R.id.toggle, ContextCompat.getColor(context, prefs.enabled ? android.R.color.holo_red_light : android.R.color.holo_green_light));
remoteViews.setTextViewText(R.id.toggle, prefs.enabled ? context.getString(R.string.widget_off) : context.getString(R.string.widget_on));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
stopSelf();
}
private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.x_drip_widget);
Log.d(TAG, "Update widget signal received");
//Add behaviour: open xDrip on click
Intent intent = new Intent(context, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.xDripwidget, pendingIntent);
displayCurrentInfo(appWidgetManager, appWidgetId, context, views);
try {
appWidgetManager.updateAppWidget(appWidgetId, views);
// needed to catch RuntimeException and DeadObjectException
} catch (Exception e) {
Log.e(TAG, "Got Rexception in widget update: " + e);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Create an Intent to launch ExampleActivity
//Intent intent = BlueService.getToggleIntent(context);
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//
Intent intent = new Intent(context, BlueToggleReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 234324243, intent, 0);
//CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.control_widget);
//Check if ON or if OFF
if( BluetoothManager.getInstance().getStatus() ){
views.setInt(R.id.widget_master, "setBackgroundResource",R.drawable.ic_audio_off );
}else{
views.setInt(R.id.widget_master, "setBackgroundResource",R.drawable.ic_audio_on );
}
views.setOnClickPendingIntent(R.id.widget_master, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(final Context context,
final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_small);
views.setOnClickPendingIntent(R.id.widget_home,
getSplashPendingIntent(context));
views.setOnClickPendingIntent(R.id.widget_write,
getWritePendingIntent(context));
views.setOnClickPendingIntent(R.id.widget_gallery,
getGalleryPendingIntent(context));
views.setOnClickPendingIntent(R.id.widget_camera,
getCameraPendingIntent(context));
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_expense);
Intent configIntent = new Intent(context, ActivityEditTransaction.class);
Transaction transaction = new Transaction(PrefUtils.getDefDepID(context));
transaction.setTransactionType(Transaction.TRANSACTION_TYPE_EXPENSE);
configIntent.putExtra("transaction", transaction);
configIntent.putExtra("update_date", true);
configIntent.putExtra("EXIT", true);
configIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri data = Uri.withAppendedPath(Uri.parse("ABCD" + "://widget/id/"),String.valueOf(appWidgetIds[0]));
configIntent.setData(data);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetContainer, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
/**
* Updates the AppWidget after the ImageView has loaded the Bitmap.
*/
private void update() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.context);
if (this.componentName != null) {
appWidgetManager.updateAppWidget(this.componentName, this.remoteViews);
} else {
appWidgetManager.updateAppWidget(this.widgetIds, this.remoteViews);
}
}
static void updateAppWidget(Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {
Log.e(TAG, "updateAppWidget Called with " + appWidgetId);
// Build the intent to call the service
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent intent = new Intent(context.getApplicationContext(),
TwitterFollowerService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// Set unique data for each pending intent, otherwise the system will
// replace the existing pending intent
intent.setAction(String.valueOf(appWidgetId));
// To react to a click we have to use a pending intent as the
// onClickListener is
// excecuted by the homescreen application
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
protected void updateWithPrayerContext(Context context, PrayerContext prayerContext) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
ComponentName cn = new ComponentName(context, getWidgetClass());
int[] appWidgetIds = mgr.getAppWidgetIds(cn);
if (appWidgetIds.length == 0) return;
for (int appWidgetId : appWidgetIds) {
RemoteViews layout = buildLayout(mgr, context, appWidgetId, prayerContext);
mgr.updateAppWidget(appWidgetId, layout);
}
}
public void setWidgetStop(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean(context.getString(R.string.widget_prefs_service_id), false).apply();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, LocationWidgetProvider.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
remoteViews.setTextViewText(R.id.tvWidgetToggle, context.getResources().getString(R.string.widget_start_text));
remoteViews.setImageViewResource(R.id.ivWidgetLocation, R.drawable.ic_widget_location_off_white_24dp);
remoteViews.setInt(R.id.llWidget, "setBackgroundResource", R.color.colorWidgetStop);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
private void completeConfiguration() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_channel);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.setAction("android.appwidget.action.APPWIDGET_UPDATE");
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
ChannelShortcut.updateWidgets(this, ChannelShortcut.class);
finish();
}
private void pushUpdate(final Context context, final int[] appWidgetIds, final RemoteViews views) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
}
private void performUpdate(Context context, AppWidgetManager awm, int[] appWidgetIds, Bundle bundle) {
for (int appWidgetId : appWidgetIds) {
Intent intent = new Intent(context, MissWidgetService.class);
intent.putExtras(bundle);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_miss_widget);
views.setRemoteAdapter(R.id.list_miss_widget, intent);
//awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.list_miss_widget);
awm.updateAppWidget(appWidgetId, views);
}
}
static void update1x1(Context context, AppWidgetManager appWidgetManager, int widgetId) {
Resources r = context.getResources();
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
Theme theme = WidgetUtils.getTheme(widgetId);
Times times = WidgetUtils.getTimes(widgetId);
if (times == null) {
WidgetUtils.showNoCityWidget(context, appWidgetManager, widgetId);
return;
}
WidgetUtils.Size size = WidgetUtils.getSize(context, appWidgetManager, widgetId, 1f);
int s = size.width;
if (s <= 0)
return;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vakit_widget);
int next = times.getNextTime();
String left = LocaleUtils.formatPeriod(now, times.getTime(today, next), false);
if (Preferences.VAKIT_INDICATOR_TYPE.get().equals("next"))
next = next + 1;
remoteViews.setOnClickPendingIntent(R.id.widget, TimesFragment.getPendingIntent(times));
Bitmap bmp = Bitmap.createBitmap(s, s, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmp);
canvas.scale(0.99f, 0.99f, s / 2f, s / 2f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(theme.bgcolor);
canvas.drawRect(0, 0, s, s, paint);
paint.setColor(theme.textcolor);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setColor(theme.hovercolor);
String city = times.getName();
paint.setColor(theme.textcolor);
float cs = s / 5f;
float ts = (s * 35) / 100f;
int vs = s / 4;
paint.setTextSize(cs);
cs = (cs * s * 0.9f) / paint.measureText(city);
cs = (cs > vs) ? vs : cs;
paint.setTextSize(vs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(Vakit.getByIndex(next).prevTime().getString(), s / 2f, (s * 22) / 80f, paint);
paint.setTextSize(ts);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(left, s / 2f, (s / 2f) + ((ts * 1) / 3), paint);
paint.setTextSize(cs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(city, s / 2f, ((s * 3) / 4f) + ((cs * 2) / 3), paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(dp);
paint.setColor(theme.strokecolor);
canvas.drawRect(0, 0, s, s, paint);
remoteViews.setImageViewBitmap(R.id.widget, bmp);
try {
appWidgetManager.updateAppWidget(widgetId, remoteViews);
} catch (RuntimeException e) {
if (!e.getMessage().contains("exceeds maximum bitmap memory usage")) {
Crashlytics.logException(e);
}
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
if (prefHelper == null)
prefHelper = new PrefHelper(context);
if (lastComicNumber == 0)
lastComicNumber = prefHelper.getRandomNumber(prefHelper.getLastComic());
else
lastComicNumber = prefHelper.getRandomNumber(lastComicNumber);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_random_layout);
Intent intent = new Intent(context, WidgetRandomProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tvAlt, pendingIntent); //Use tvAlt instead of shuffle if available
Intent intent2 = new Intent("de.tap.easy_xkcd.ACTION_COMIC");
intent2.putExtra("number", lastComicNumber);
PendingIntent openInApp = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.ivComic, openInApp);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
RealmComic comic = (new DatabaseManager(context)).getRealmComic(lastComicNumber);
AppWidgetTarget appWidgetTarget = new AppWidgetTarget(context, R.id.ivComic, remoteViews, appWidgetIds) {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
super.onResourceReady(resource, transition);
} catch (IllegalArgumentException e) {
Timber.e(e, "Loading image failed for %d", comic.getComicNumber());
}
}
};
if (comic != null) {
if (prefHelper.fullOfflineEnabled()) {
remoteViews.setImageViewBitmap(R.id.ivComic, RealmComic.getOfflineBitmap(lastComicNumber, context, prefHelper));
} else {
GlideApp.with(context)
.asBitmap()
.load(comic.getUrl())
.into(appWidgetTarget);
}
String title = prefHelper.widgetShowComicNumber() ? (lastComicNumber + ": ") : "";
remoteViews.setTextViewText(R.id.tvTitle, title + comic.getTitle());
remoteViews.setTextViewText(R.id.tvAlt, comic.getAltText());
if (prefHelper.widgetShowAlt())
remoteViews.setViewVisibility(R.id.tvAlt, View.VISIBLE);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int appWidgetId : appWidgetIds) {
String name = prefs.getString("widget." + appWidgetId + ".name", null);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L);
String type = prefs.getString("widget." + appWidgetId + ".type", null);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int font = prefs.getInt("widget." + appWidgetId + ".font", 0);
int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0);
Intent view = new Intent(context, ActivityView.class);
view.setAction("folder:" + folder);
view.putExtra("account", account);
view.putExtra("type", type);
view.putExtra("refresh", true);
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_unified);
if (!semi)
views.setInt(R.id.widget, "setBackgroundColor", Color.TRANSPARENT);
if (font > 0)
views.setTextViewTextSize(R.id.title, TypedValue.COMPLEX_UNIT_SP, getFontSizeSp(font));
if (padding > 0) {
int px = getPaddingPx(padding, context);
views.setViewPadding(R.id.title, px, px, px, px);
}
if (name == null)
views.setTextViewText(R.id.title, context.getString(R.string.title_folder_unified));
else
views.setTextViewText(R.id.title, name);
views.setOnClickPendingIntent(R.id.title, pi);
Intent service = new Intent(context, WidgetUnifiedService.class);
service.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
service.setData(Uri.parse(service.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.lv, service);
Intent thread = new Intent(context, ActivityView.class);
thread.setAction("widget");
thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(type));
thread.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piItem = PendingIntent.getActivity(
context, ActivityView.REQUEST_WIDGET, thread, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.lv, piItem);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
private static void updateWidget(Context context, AppWidgetManager manager, int appWidgetId, boolean serviceRunning) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_recording);
// change the subtext depending on whether the service is running or not
CharSequence subtext = context.getText(
serviceRunning ? R.string.widget_recording_in_progress : R.string.widget_start_recording);
updateViews.setTextViewText(R.id.widget_subtext, subtext);
// if service not running, don't show the "recording" icon
updateViews.setViewVisibility(R.id.record_badge_image_view, serviceRunning ? View.VISIBLE : View.INVISIBLE);
PendingIntent pendingIntent = getPendingIntent(context, appWidgetId);
updateViews.setOnClickPendingIntent(R.id.clickable_linear_layout, pendingIntent);
manager.updateAppWidget(appWidgetId, updateViews);
}
private static void updateWidget(Context context, AppWidgetManager manager, int appWidgetId, boolean serviceRunning) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_recording);
// change the subtext depending on whether the service is running or not
CharSequence subtext = context.getText(
serviceRunning ? R.string.widget_recording_in_progress : R.string.widget_start_recording);
updateViews.setTextViewText(R.id.widget_subtext, subtext);
// if service not running, don't show the "recording" icon
updateViews.setViewVisibility(R.id.record_badge_image_view, serviceRunning ? View.VISIBLE : View.INVISIBLE);
PendingIntent pendingIntent = getPendingIntent(context, appWidgetId);
updateViews.setOnClickPendingIntent(R.id.clickable_linear_layout, pendingIntent);
manager.updateAppWidget(appWidgetId, updateViews);
}
void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_channel);
Log.d(TAG, "Update the widget " + appWidgetId);
// Get the widget id to get the channel
final JsonChannel channel = WidgetSelectionActivity.getWidgetChannel(context, appWidgetId);
if (channel == null) {
views.setTextViewText(R.id.widget_text, "Reconfigure this widget");
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Log.d(TAG, "Loading the image " + channel.getLogo());
final Bitmap logo = Glide.with(context)
.load(ChannelDatabase.getNonNullChannelLogo(channel))
.asBitmap()
.placeholder(R.drawable.c_banner_3_2)
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Update the bitmap");
views.setImageViewBitmap(R.id.widget_image, logo);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
});
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}).start();
Log.d(TAG, channel.getNumber() + " " + channel.getName());
views.setTextViewText(R.id.widget_text, channel.getNumber() + " " + channel.getName());
Intent i = new Intent(context, CumulusVideoPlayback.class);
i.putExtra(CumulusVideoPlayback.KEY_VIDEO_URL, channel.getMediaUrl());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}