我试图每30分钟更新一次AppWidget中的ImageView,但是没有运气。
它使用Glide库加载小部件,我怀疑是缓存。
我正在竭尽全力防止干扰唤醒和电池。只是需要不时更改图像。醒来就可以了。
我认为该设备处于活动状态时需要进行30分钟的轮询。就像,当它睡觉时,它不会计数并且会错过它。
谢谢您的帮助。
res / xml / widget_name.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget_name"
android:initialLayout="@layout/widget_name"
android:minWidth="300dp"
android:minHeight="100dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen"></appwidget-provider>
com / WidgetName / WidgetName.java
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_name);
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.imageView, remoteViews, appWidgetIds) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
}
};
RequestOptions options = new RequestOptions().
override(300, 300).placeholder(R.drawable.navigation_header_bg).error(R.drawable.ic_navigation_help);
// Create an Intent to launch Browser
Intent intent = new Intent(context, WidgetConfig.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, intent, 0);
Log.i("WidgetName", "src " + src);
remoteViews.setOnClickPendingIntent(R.id.imageView, pendingIntent);
Glide.with(context.getApplicationContext())
.asBitmap()
.load(src)
.apply(options)
.into(awt);
}
答案 0 :(得分:1)
在研究项目时,您需要进行以下更改。
在res / xml / widget_name.xml中设置为android:updatePeriodMillis="0"
然后在com / WidgetName / WidgetName.java中
重写onReceive方法并进行以下更改
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case AppWidgetManager.ACTION_APPWIDGET_UPDATE:
onUpdate(context, AppWidgetManager.getInstance(context), AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, LineaMeteoPremium.class)));
break;
default:
super.onReceive(context, intent);
}
}
并且由于您要更新所有小部件,因此按如下循环依次更新它们,将 AlarmManager 用作刷新触发器(更好的灵活性,因为您也可以允许用户设置自定义刷新时间)另一个问题是您没有使用 skipMemoryCache(true)和 diskCacheStrategy(DiskCacheStrategy.NONE)方法来强制滑动获取新图像,而不是查找缓存。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
setUpAlarm(context, widgetId);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.linea_meteo_premium);
Log.e("LineaMeteo Widget", "src " + src);
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.imageView, remoteViews, appWidgetIds);
RequestOptions options = new RequestOptions().
override(300, 300).placeholder(R.drawable.navigation_header_bg).error(R.drawable.ic_navigation_help).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
// Create an Intent to launch Browser
Intent intent = new Intent(context, WidgetConfig.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.imageView, pendingIntent);
Glide.with(context.getApplicationContext())
.asBitmap()
.load(src)
.apply(options)
.into(awt);
}
}
使用以下代码根据需要的时间设置警报以刷新小部件
/**
* Sets up widget refresh alarm
*
* @param context The context of widget
* @param appWidgetId Widget ID of current widget
*/
private void setUpAlarm(Context context, int appWidgetId) {
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long interval = (3 * 60000); // 60000 = 1minute | Change the formula based on your refresh timing needs
PendingIntent alarmPendingIntent = getRefreshWidgetPendingIntent(context, appWidgetId);
alarm.cancel(alarmPendingIntent);
// SET NEW ALARM
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + interval, alarmPendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarm.setExact(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + interval, alarmPendingIntent);
} else {
alarm.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + interval, alarmPendingIntent);
}
}
/**
* Get the pending intent object for refreshing the widget
*
* @param context current Context
* @param widgetId - Current Widget ID
* @return - Pending intent for refreshing widget
*/
private PendingIntent getRefreshWidgetPendingIntent(Context context, int widgetId) {
Intent intent = new Intent(context, LineaMeteoPremium.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
// Make the pending intent unique...
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
return PendingIntent.getBroadcast(context, 123, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
最重要的是,由于您的应用程序正在使用ClearText HTTP Traffic,并且默认情况下从Android 9.0开始禁用明文支持,因此您需要在清单的应用标签中添加以下标签,以防止Widget在Android 9. +设备上崩溃。< / p>
<application
.
.
android:usesCleartextTraffic="true">