collection disappears on kitkat
I developed a widget for Android, using the listview-collection (only with one entry in order to enable scrolling). It updates itself every day. It works well, but it disappears sometimes. I don't get any error message in the LogCat and I can darg it around and resize it, but it's transparent. But after some hours it appears again.
That bug appears on a Smartphone with Android 4.1.2 (when I rotate the screen, too) and on an Android 4.4.2 Tablet
some Code:
AppWidgetProvider:
public class Widget extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
//set first alarm
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+100, pi);
}
@SuppressLint("NewApi") @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,Widget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for(int widgetId: allWidgetIds){
//which layout to show on widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
//RemoteViews Service needed to provide adapter for ListView
Intent svcIntent = new Intent(context, WidgetService.class);
//passing app widget id to that RemoteViews Service
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
//setting adapter to listview of the widget
remoteViews.setRemoteAdapter(R.id.listViewWidget,svcIntent);
//setting views
remoteViews.setEmptyView(R.id.listViewWidget, R.id.update);
remoteViews.setTextViewText(R.id.update_list_head, getText(context)[0]);
remoteViews.setTextViewText(R.id.update_list_content, getText(context)[1]);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public static String[] getText(Context context){
String[] text = null;
//read a file ...
return text;
}
}
getViewAt(int position) in RemoteViewsFactory
@Override
public RemoteViews getViewAt(int position) {
SharedPreferences shPref = PreferenceManager.getDefaultSharedPreferences(context);
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
int bgColor = Color.parseColor(shPref.getString("background-list-widget", "#E8E8E8"));
remoteView.setInt(R.id.list_row, "setBackgroundColor", bgColor);
int textColor = Color.parseColor(shPref.getString("color-list-widget", "#000000"));
remoteView.setInt(R.id.update_list_content, "setTextColor", textColor);
remoteView.setInt(R.id.update_list_head, "setTextColor", textColor);
float size;
try {
size = Float.parseFloat(shPref.getString("text-size-widget", "12"));
} catch (Exception e) {
size = 12;
}
remoteView.setFloat(R.id.update_list_content, "setTextSize", size);
remoteView.setFloat(R.id.update_list_head, "setTextSize", size);
remoteView.setTextViewText(R.id.update_list_head, Widget.getText(context)[0]);
remoteView.setTextViewText(R.id.update_list_content,Widget.getText(context)[1]);
return remoteView;
}
BroadcastReceiver:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
@SuppressLint({ "Wakelock", "NewApi" }) @Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "updateWidget");
//Acquire the lock
wl.acquire();
//update remote views.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.update);
remoteViews.setTextViewText(R.id.update_list_head, Widget.getText(context)[0]);
remoteViews.setTextViewText(R.id.update_list_content, Widget.getText(context)[1]);
ComponentName thiswidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thiswidget, remoteViews);
//Release the lock
wl.release();
//set next alarm
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent mIntent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
}
}
Thank you!
我通过使用notifydatasetchanged()解决了这个问题
链接地址: http://www.djcxy.com/p/8940.html上一篇: Android主屏幕小部件神秘失败,无法接收触摸事件
下一篇: 集合在kitkat上消失