2013-07-17 191 views
2

當我點擊它時,我試圖改變我的小部件視圖的圖像,但它不起作用。這是我的代碼:小部件中的setOnClickPendingIntent不起作用

public class myAppWidgetProvider extends AppWidgetProvider { 

     public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh"; 
     //... 

    @Override 
     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]; 

      Intent intent = new Intent(context, myAppWidgetProvider.class); 
      intent.setAction(ACTION_WIDGET_REFRESH); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
      RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
      rv.setOnClickPendingIntent(R.id.imageView, pendingIntent); 
      appWidgetManager.updateAppWidget(appWidgetId, rv); 
      } 

而在的onReceive我希望得到它,當我點擊小工具的觀點,但它不工作:

@Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 

     final String action = intent.getAction(); 
     if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) { 

      RemoteViews rmv = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
        //and this change doesn't work :((\/ 
      rmv.setImageViewResource(R.id.imageView, R.drawable.button_power_on_small); 
     } 
    } 

這是我的Android清單接收器:

<receiver android:name="myAppWidgetProvider" android:exported="false" android:icon="@drawable/button_power_off"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     <action android:name="com.xxx.xxx.myAppWidgetProvider.ACTION_WIDGET_REFRESH"/> 
     <action android:name="android.appwidget.action.APPWIDGET_DELETED"/> 
     <action android:name="android.media.RINGER_MODE_CHANGED"/> 
</intent-filter> 
    <meta-data android:name="android.appwidget.provider" 
       android:resource="@xml/widget_info_provider" /> 
</receiver> 

如果有人向我推薦一些解決方案,我將不勝感激。

+0

試一下:rmv.setInt(R.id.imageView,「setImageResource」,R.drawable.button_power_on_small); – Opiatefuchs

+0

它也不起作用,但我不知道當我單擊小部件時是否調用onReceive方法。我認爲不是,因爲onReceive中的方法應該可以工作 – user2511941

+0

有幫助的是在onReceive()上執行日誌。然後你會知道這是否被調用,直到該方法工作的時間點。首先在super()之後直接登錄onReceive,第二個應該在你的if子句中。如果兩者都被調用您可以確定您的remoteViews聲明中一定存在錯誤。示例onReceive:Log.d(「keyword」,「Action:onReceive」);示例if子句:Log.d(「keyword」,「Action:if子句」); – Opiatefuchs

回答

2

變化

<action android:name="com.xxx.xxx.myAppWidgetProvider.ACTION_WIDGET_REFRESH"/>

<action android:name="ActionReceiverRefresh"/>

你的廣播接收器是找錯了操作字符串。請記住,AndroidManifest是一個XML文件。你必須把字符串的實際值;指向一個Java常量不起作用。

+0

謝謝你這個好建議,但我取代if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))if ACTION_WIDGET_REFRESH.equals(action)),它也正常工作。 – user2511941

+0

好的,就像我不得不改變視圖圖像的悲傷方法不起作用。該方法:rmv.setInt(R.id.imageView,「setImageResource」,R.drawable.button_power_on_small);和rmv.setImageViewResource(R.id.imageView,R.drawable.button_power_on_small);做某人知道爲什麼? – user2511941

相關問題