2

写下代码以更新小部件。

此代码始终有效

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();
    }
}
4

1 回答 1

2

也许您使用的 Context 类型不正确。只使用传递给函数的上下文,不要使用类中声明的私有上下文。

public void onReceive(Context context, Intent intent)
于 2012-12-17T13:02:00.130 回答