2011-08-04 62 views
3

嗨IM,我已經在很多地方extensivly看了,但我似乎無法弄清楚什麼是錯。基本上我正在調用我的小部件中的pendingintent廣播,併成功捕獲onrecivie方法中的意圖。的Android Appwidget的TextView沒有更新和我的Android小工具一個非常奇怪的問題

然而在onRecive方法,當我嘗試設置使用RemoteViews我的組件中的文本,該文本不更新,也不是所謂的任何錯誤。我附上了我的代碼,任何幫助都會很棒。

感謝, 中號

package com.android.FirstWidget;import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.RemoteViews; 

public class ExampleAppWidgetProvider extends AppWidgetProvider { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     Log.e("ds",intent.getAction()); 
     Log.e("f","f"); 
     if(intent.getAction().contains("1")){ 

      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); 
      views.setTextViewText(R.id.textView1,"heyheyhey"); 
      Log.e("fssss","sssf"); 
     } 
     super.onReceive(context, intent); 
    } 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      // Create an Intent to launch ExampleActivity 
      Intent intent = new Intent(context, ExampleAppWidgetProvider.class); 
      intent.setAction("1"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
      // Get the layout for the App Widget and attach an on-click listener 
      // to the button 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); 
      views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent); 
      views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent); 
//views.set 
      // Tell the AppWidgetManager to perform an update on the current app widget 
      appWidgetManager.updateAppWidget(appWidgetId, views); 

     } 

    } 
} 

回答

0

當您覆蓋的onReceive,所有意向廣播是由打電話的onReceive。 onUpdate,onDestroy等,所有其他的都是而不是調用了。

獲取的onReceive意圖的行動,以確定它是什麼類型的意圖,你應該用廣播做什麼。

+0

是的,我得到正確的行動,只有然後我調用views.setTextViewText,但仍然textview將不會更新 – Marius

+0

我是害怕你錯了。你粘貼的代碼的onUpdate方法將不會被任何時間調用,因爲你已經覆蓋onReceive。 onReceive取代所有其他on-methods。此外,我所說的「行動」是這4個行動:http://developer.android.com/guide/topics/appwidgets/index.html#ProviderBroadcasts –

0

您正在創建RemoteViews並在onReceive中設置textview,但從未調用AppWidgetManager.updateAppWidget(),因此整個操作完全沒有意義,小部件將不會更新,因爲它不會發送遠程視圖。

在onUpdate()中,您正確地創建了RemoteViews(但未設置TextView)並調用AppWidgetManager.updateAppWidget(),所以這將正確設置按鈕的pendingintents,但不設置textview。

解決方案:設置TextView的調用後AppWidgetManager.updateAppWidget()。 注1:也要重新設置等待,否則按鈕將死(IIRC)。 注2:每個基於onupdate的timer都會再次清除textview(IIRC)。

7

你需要幾行代碼添加到onRecieve方法結束....它應該是這樣..

package com.android.FirstWidget;import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.RemoteViews; 

public class ExampleAppWidgetProvider extends AppWidgetProvider { 


    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     Log.e("ds",intent.getAction()); 
     Log.e("f","f"); 

     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); 
     if(intent.getAction().contains("arrow_left")){ 
      views.setTextViewText(R.id.textView1,"left"); 
      Log.e("fssss","sssf"); 
     } 
     else if(intent.getAction().contains("arrow_right")){ 
      views.setTextViewText(R.id.textView1,"right"); 
      Log.e("fssss","sssf"); 
     } 

     else { 
      super.onReceive(context, intent); 
     } 
     ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class); 
     AppWidgetManager.getInstance(context).updateAppWidget(componentName, views); 
    } 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      // Create an Intent to launch ExampleActivity 
      Intent intent = new Intent(context, ExampleAppWidgetProvider.class); 
      intent.setAction("arrow_left"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
      // Get the layout for the App Widget and attach an on-click listener 
      // to the button 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); 
      views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent); 

      intent = new Intent(context, ExampleAppWidgetProvider.class); 
      intent.setAction("arrow_right"); 
      pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
      views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent); 
//views.set 
      // Tell the AppWidgetManager to perform an update on the current app widget 
      appWidgetManager.updateAppWidget(appWidgetId, views); 

     } 

    } 
} 

而且雖然我相信你已經編寫什麼應該工作,但如果沒有你可以嘗試將清單文件中按鈕的意圖操作。它應該是這樣的

<intent-filter > 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
     <action android:name="com.android.FirstWidget.arrow_left"/> 
     <action android:name="com.android.FirstWidget.arrow_right"/> 
</intent-filter> 

我想這應該現在工作。我只是偶然發現自己陷入了這個問題,並找到了解決辦法:Clickable widgets in android

+0

我將如何使用您的更新我的代碼?我試圖從JSON文件顯示textview中的文本:http://stackoverflow.com/questions/20100117/widget-is-not-updating-after-json-parse – Si8