Android 小部件更新不起作用

Posted

技术标签:

【中文标题】Android 小部件更新不起作用【英文标题】:Android widgets update doesn't work 【发布时间】:2012-11-29 20:49:39 【问题描述】:

编写代码以更新小部件。

此代码始终有效

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

【问题讨论】:

调试和/或单元测试你 shuffle 方法,似乎有一些错误(而 c == null) 不工作。Tost 为每次点击显示其他 id imgShaffleWidget 。 【参考方案1】:

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

public void onReceive(Context context, Intent intent)

【讨论】:

以上是关于Android 小部件更新不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Android setTextViewText 不起作用

Android小部件ListView项目点击不起作用

小部件onClick中的Android ListView不起作用

PHP - Wordpress - 插件小部件更新功能 - 更新数组值 [Foreach 循环不起作用]

Android应用模拟时钟小部件不起作用

通过单击整个小部件来启动活动在 Android Studio 中不起作用