2012-03-18 41 views
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

你需要調用Toast.makeText從UI線程。你可以通過做:

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

+1,'runOnUiThread()'是不是靜態方法:) – 2012-03-18 12:54:34

+1

沒有'runOnUiThread'上'AppWidgetProvider'實例既不 – melanke 2016-01-07 14:20:47