2012-03-18 5 views
3

Webサービス(スレッド内のonReceive())からデータをクリックしてダウンロードできるappwidgetがあります。これが終了したら、ウィジェットの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

あなたはUIスレッドからToast.makeTextを呼び出す必要があります。あなたは実行していることを行うことができます `AppWidgetProvider`インスタンスには` runOnUiThread`はありません

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

+1を、 'runOnUiThreadは()'静的メソッドではありません:) –

+1

どちらもありません – melanke

関連する問題