3
我有一个appwidget,它可以通过单击它从Web服务(线程中的onReceive()
)下载一些数据。在完成之后,小部件的GUI将在updateWidget(...)
(=重新绘制)中更新。无法从AppWidgetProvider类中的线程进行烘烤
我想在这样做时敬酒。我很想在updateWidget(...)
的最后通过从线程的上下文传递给敬酒,但这没有奏效。问题似乎是上下文。因为我的班级从AppWidgetProvider
继承,而不是来自活动,所以不能使用类似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,'runOnUiThread()'是不是静态方法:) – 2012-03-18 12:54:34
没有'runOnUiThread'上'AppWidgetProvider'实例既不 – melanke 2016-01-07 14:20:47