gpt4 book ai didi

android - 无法从 AppWidgetProvider 类中的线程 toast

转载 作者:太空狗 更新时间:2023-10-29 14:28:10 24 4
gpt4 key购买 nike

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

完成后我想 toast 。我尝试了很多方法,比如在 updateWidget(...) 结束时通过将上下文从线程传递到 toast 来 toast ,但这没有用。问题似乎是上下文。因为我的类继承自 AppWidgetProvider 而不是 Activity 我不能使用像 getApplicationContext() 这样的东西。我想我只需要为 toast 找到正确的上下文,但我不知道该怎么做。

相关代码如下:

@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!!!
}

最佳答案

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

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

关于android - 无法从 AppWidgetProvider 类中的线程 toast ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com