2012-03-09 60 views
0

我的應用程序上有一個小部件,Service爲了自動更新。如何處理按鈕單擊小工具

小工具有ImageButton其中觸發手動更新內容。但我無法處理點擊事件。

這裏是我的AppWidgetProvider

public class MyWidget extends AppWidgetProvider { 

    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

     RemoteViews rmViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

     Intent active = new Intent(context, MyWidget.class); 
     active.setAction(ACTION_WIDGET_RECEIVER); 
     active.putExtra("msg", "Message for Button 1"); 
     PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, active, 0); 

     rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent); 

     appWidgetManager.updateAppWidget(appWidgetIds, rmViews); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 
      if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
       Log.d(debug_tag, intent.getAction()); 
      } 
    } 
} 

當我點擊按鈕沒有什麼改變。它甚至沒有記錄。

的Manifest.xml聲明:

<receiver android:name=".MyWidget" android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     <action android:name="com.app.example.MyWidget.ACTION_WIDGET_RECEIVER"/> 
     </intent-filter> 
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget"/> 
</receiver> 

有什麼不對?

順便說一句,如果我可以處理點擊,我應該如何使用手動更新?我是否將其傳遞給服務以及哪種方式?

編輯:更改getActivity()getBroadcast()並處理好點擊。但無法將此消息傳遞給服務。任何建議?

回答

2
Update your `AppWidgetProvider`: 


public class MyWidget extends AppWidgetProvider { 
     public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; 

     public static int appid[]; 
     public static RemoteViews rview; 
     @Override 
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
       int[] appWidgetIds){ 
      updateWidgetState(context, ""); 
     } 
     @Override 
     public void onReceive(Context paramContext, Intent paramIntent) 
      { 
      String str = paramIntent.getAction(); 
      if (paramIntent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
       updateWidgetState(paramContext, str); 
      } 
      else 
      { 
        if ("android.appwidget.action.APPWIDGET_DELETED".equals(str)) 
         { 
         int i = paramIntent.getExtras().getInt("appWidgetId", 0); 
         if (i == 0) 
         { 

         } 
         else 
         { 
          int[] arrayOfInt = new int[1]; 
          arrayOfInt[0] = i; 
          onDeleted(paramContext, arrayOfInt); 
         } 
         } 
       super.onReceive(paramContext, paramIntent); 
      } 
      } 
     static void updateWidgetState(Context paramContext, String paramString) 
      { 
      RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); 
      ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class); 
      AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews); 
      } 
     private static RemoteViews buildUpdate(Context paramContext, String paramString) 
      { 
      // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show(); 
      rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout); 
      Intent active = new Intent(paramContext, MyWidget.class); 
      active.setAction(ACTION_WIDGET_RECEIVER); 
      active.putExtra("msg", "Message for Button 1"); 
      PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0); 
      rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent); 
      if(parmString.equals(ACTION_WIDGET_RECEIVER)) 
      { 
      //your code for update and what you want on button click 

      } 
      return rview; 
      } 
     @Override 
     public void onEnabled(Context context){ 
      super.onEnabled(context); 
      // Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show(); 
     } 
     // Called each time an instance of the App Widget is removed from the host 
     @Override 
     public void onDeleted(Context context, int [] appWidgetId){ 
      super.onDeleted(context, appWidgetId); 
      // Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show(); 
     } 
     // Called when last instance of App Widget is deleted from the App Widget host. 
     @Override 
     public void onDisabled(Context context) { 
      super.onDisabled(context); 
      // Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show(); 
     } 

    } 
+0

好了我如何做一些細節,但我已經爲自然而然更新服務,這很重要? – 2012-03-09 11:43:54

+0

@Ogulcan:不,不要緊,只是爲了改進'AppWidgetProvider'類,因爲當你想更新小部件時你沒有區分 – 2012-03-09 11:59:31

-1
public class MyWidget extends AppWidgetProvider { 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    RemoteViews remoteViews; 
    ComponentName watchWidget; 

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget_layout); 
    watchWidget = new ComponentName(context, MyWidget .class); 
    remoteViews.setOnClickPendingIntent(R.id.update, getPendingSelfIntent(context, SYNC_CLICKED)); 
    appWidgetManager.updateAppWidget(watchWidget, remoteViews); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    super.onReceive(context, intent); 

    if (SYNC_CLICKED.equals(intent.getAction())) { 

     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

     RemoteViews remoteViews; 
     ComponentName watchWidget; 

     remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget_layout); 
     watchWidget = new ComponentName(context, MyWidget .class); 

     remoteViews.setTextViewText(R.id.update, "TESTING"); 
     customToast.showToast("manage your click here" ,context); 
     appWidgetManager.updateAppWidget(watchWidget, remoteViews); 

    } 
} 

protected PendingIntent getPendingSelfIntent(Context context, String action) { 
    Intent intent = new Intent(context, getClass()); 
    intent.setAction(action); 
    return PendingIntent.getBroadcast(context, 0, intent, 0); 
} 
+2

代碼只有答案被認爲是不好的答案,請解釋它做了什麼? – YoungHobbit 2015-12-13 15:19:16

+1

查看關於元代碼的答案的討論:http://meta.stackexchange.com/questions/148272 – cuihtlauac 2015-12-13 15:20:12