2
編寫代碼來更新小部件。Android小部件更新不起作用
此代碼的工作總是
Toast.makeText(context, ""+cd.getId(), Toast.LENGTH_LONG).show();
但幽冥代碼只有當它是調試工作。
@Override
public void onUpdate(Context context, AppWidgetManager appwidgetmanager,
int[] appWidgetIds) {
for (int i : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget4x4persian);
shaffle(context, appwidgetmanager, i, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
super.onReceive(context, intent);
ComponentName thisWidget = new ComponentName(context, gift4x4.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget4x4persian);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
clickWidget(context, appWidgetManager, intent, remoteViews);
}
private void clickWidget(Context context,
AppWidgetManager appWidgetManager, Intent intent,
RemoteViews remoteViews) {
Cursor c = null;
try {
ComponentName thisWidget = new ComponentName(context, gift4x4.class);
Bundle b = intent.getExtras();
if (b != null) {
int key = b.getInt(WIDGET_KEY);
String id = null;
int[] allWidgetIds = appWidgetManager
.getAppWidgetIds(thisWidget);
for (int i : allWidgetIds) {
RemoteViews views = new RemoteViews(
context.getPackageName(),
R.layout.widget4x4persian);
shaffle(context, appWidgetManager, i, remoteViews);
}
}
} catch (NumberFormatException e) {
e.getMessage();
} finally {
if (c != null)
c.close();
}
}
public void shaffle(Context context, AppWidgetManager appwidgetmanager,
int appWidgetId, RemoteViews Rview) {
Cursor c = null;
try {
Card cd = null;
do {
PreferenceManager pm = new PreferenceManager(context);
Cards_Count = pm.get(PREFNAME_CARDS_COUNTS, CARDS_COUNTS);
int shaffle = (new Random()).nextInt(Cards_Count);
c = context.getContentResolver().query(CONTENT_URI_CARDS,
new String[] { ID, TEXT, ISREAD, ISBOOKMARK, NOTE },
"id=" + shaffle, null, null);
if (c.moveToFirst()) {
cd = new Card(c.getInt(c.getColumnIndex(ID)), c.getString(c
.getColumnIndex(TEXT)), c.getInt(c
.getColumnIndex(ISREAD)), c.getInt(c
.getColumnIndex(ISBOOKMARK)), c.getString(c
.getColumnIndex(NOTE)));
}
c.close();
} while (c == null);
Rview.setImageViewResource(R.id.imgIsBookmarkWidget, cd
.getIsBookmark() == 1 ? R.drawable.bookmark
: R.drawable.unbookmark);
Bitmap bmp = getPicture(context, cd.getId());
Toast.makeText(context, ""+cd.getId(), Toast.LENGTH_LONG).show();
Rview.setImageViewBitmap(R.id.imgWidget, bmp);
Intent intentTemp = new Intent(context, gift4x4.class);
intentTemp.putExtra(WIDGET_KEY, key);
PendingIntent pendingintentTemp = PendingIntent.getBroadcast(context, key,
intentTemp, PendingIntent.FLAG_UPDATE_CURRENT);
Rview.setOnClickPendingIntent(R.id.imgShaffleWidget,
getProperIntent(context, 3, cd));
appwidgetmanager.updateAppWidget(appWidgetId, Rview);
} catch (Exception e) {
e.getMessage();
} finally {
if (c != null)
c.close();
}
}
調試和/或單元測試,似乎有一些bug(而C == NULL) – Snicolas
不工作.Tost顯示其它ID爲每次點擊imgShaffleWidget。 –