无法从AppWidgetProvider类中的线程进行吐司

时间:2012-03-18 10:55:56

标签: android toast android-appwidget android-context

我有一个appwidget,可以通过点击它从Web服务(onReceive()中的一个线程)下载一些数据。完成后,窗口小部件的GUI将在updateWidget(...)(=重新绘制)中更新。

我想在这完成后举杯祝酒。我尝试通过将上下文从线程传递给toast来在updateWidget(...)结束时做多敬酒,但这不起作用。问题似乎是背景。因为我的类继承自AppWidgetProvider而不是来自Activity I,所以不能使用像getApplicationContext()这样的东西。我想我只需要为吐司获得正确的上下文,但我不知道该怎么做。

以下是相关代码:

@Override
public void onReceive(Context context, Intent intent)
{
    //  Name of the action of the intent
    final String action = intent.getAction();

    //  This way the context and the intent can be used in the thread
    final Context ctx = context;
    final Intent i = intent;

    if (action.equals(COUNT_DOWN) == true || action.equals(COUNT_UP) == true)
    {
        //  Show toast to inform the user that this feature is only available in the full version
        Toast.makeText(context, R.string.no_groupchange, Toast.LENGTH_SHORT).show();
    }

    // Update current group
    else if (action.equals(UPDATE_WIDGET))
    {
        // Check for internet connection
        if (isOnline(context) == true)
        {
            //  Show toast to inform the user that refreshing the data has started
            Toast.makeText(context, R.string.refreshing, Toast.LENGTH_SHORT).show();

            //  This way the complete internet communication is independent from the GUI
            //  Also works at low speed internet connection
            Thread myThread = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    // TODO Auto-generated method stub
                    int currentGroupOrderID = soapManager.getCurrentGroupOrderID(LEAGUE_SC);
                    theGroup = soapManager.getGroup(currentGroupOrderID, LEAGUE_SC, SAISON);
                    nextMatch = soapManager.getNextMatch(LEAGUE_SC);

                    updateWidget(ctx, i);                       
                }
            });

            myThread.start();               
        } 
        else
        {
            Toast.makeText(context, R.string.no_internet, Toast.LENGTH_SHORT).show();
        }
    }

    super.onReceive(context, intent);
}

public void updateWidget(Context context, Intent intent)
{           
    //  Creating remoteViews for updating the widget
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout_simple);

    //  Fill the textViews with text
    setTextViewsFromTheGroup(remoteViews);
    setNextMatchTextView(remoteViews);

    //  Update the widget
    ComponentName widget = new ComponentName(context, SoccerWidgetProvider.class);
    AppWidgetManager awm = AppWidgetManager.getInstance(context);
    awm.updateAppWidget(widget, remoteViews);

    //  This way the widget doesn't stop reacting after some time
    final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
    {
        attachIntents(context, remoteViews, appWidgetId);
    }

    //  HERE I WANT TO MAKE A TOAST!!!
}

1 个答案:

答案 0 :(得分:1)

您需要从UI线程中调用Toast.makeText。你可以这样做:

runOnUiThread(new Runnable() {
    public void run()
    {
        Toast.makeText(context, R.string.refreshing, Toast.LENGTH_SHORT).show();
    }
});