Android小部件更新不起作用

时间:2012-12-12 07:27:39

标签: java android android-widget android-pendingintent

写下代码以更新小部件。

此代码始终

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

1 个答案:

答案 0 :(得分:2)

您使用的上下文可能不是正确的类型。 只需使用传递给函数的上下文,不要使用在类中声明的私有上下文。

public void onReceive(Context context, Intent intent)