【问题标题】:Can't make a toast from thread in an AppWidgetProvider class无法从 AppWidgetProvider 类中的线程敬酒
【发布时间】:2012-03-18 10:55:56
【问题描述】:

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

完成后我想祝酒。我很想在updateWidget(...) 的末尾通过将上下文从线程传递给 toast 来祝酒,但这没有用。问题似乎是上下文。因为我的类继承自 AppWidgetProvider 而不是 Activity 我不能使用像 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!!!
}

【问题讨论】:

    标签: android toast android-appwidget android-context


    【解决方案1】:

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

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

    【讨论】:

    • AppWidgetProvider 实例上也没有 runOnUiThread
    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 2020-01-13
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2018-01-28
    • 2012-03-14
    相关资源
    最近更新 更多