知道单击了哪个按钮,请按照下列步骤操作:
step 1:为 btnext 和 btn Prev 注册两个接收器
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_NEXT"/>
<action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_PREV"/>
</intent-filter>
step 2:对于 btnNext 和 btnPrev 操作字符串:
public class MyWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICK_NEXT = "Action_nextbtn";
public static String ACTION_WIDGET_CLICK_PREV = "Action_prevbtn";
}
step 3:在 onReceive 中
@Override
public void onReceive(Context paramContext, Intent paramIntent)
{
String str = paramIntent.getAction();
if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_NEXT)) {
updateWidgetState(paramContext, str);
}
if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_PREV)) {
updateWidgetState(paramContext, str);
}
}
step 4:创建一个更新小部件状态的方法:
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);
}
Step 5:
private static RemoteViews buildUpdate(Context paramContext, String paramString)
{
rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
Intent activebtnnext = new Intent(paramContext, MyWidget.class);
active.setAction(ACTION_WIDGET_CLICK_NEXT);
PendingIntent configPendingIntentnext = PendingIntent.getBroadcast(paramContext, 0, activebtnnext , 0);
rmViews.setOnClickPendingIntent(R.id.btnNext, configPendingIntentnext);
Intent activeprevbtn = new Intent(paramContext, MyWidget.class);
active.setAction(ACTION_WIDGET_CLICK_PREV);
PendingIntent configPendingIntentprev = PendingIntent.getBroadcast(paramContext, 0, activeprevbtn , 0);
rmViews.setOnClickPendingIntent(R.id.btnprev, configPendingIntentprev);
if(parmString.equals(ACTION_WIDGET_CLICK_NEXT))
{
//
}
if(parmString.equals(ACTION_WIDGET_CLICK_PREV))
{
//
}
return rview;
}