我試圖設置一個包含兩個按鈕的Widget。點擊小部件應該提高主要活動。點擊兩個按鈕中的一個應該執行另一個操作。Android Widget上的按鈕操作
我試過按照Android widget button和Processing more than one button click at Android Widget上描述的步驟,但沒有骰子。我不知道我做錯了什麼。
我的清單的相關部分:
<receiver android:name="WidgetProvider" android:icon="@drawable/icon" android:label="Radio Podcastellano">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="es.radiopodcastellano.player.WidgetProvider.ACTION_UPDATEINFO"/>
<action android:name="es.radiopodcastellano.player.WidgetProvider.ACTION_REFRESH"/>
<action android:name="es.radiopodcastellano.player.WidgetProvider.ACTION_PLAYPAUSE"/>
<action android:name="es.radiopodcastellano.player.WidgetProvider.ACTION_SHOWMAIN" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" />
</receiver>
現在,WidgetProvider類本身:
public class WidgetProvider extends AppWidgetProvider {
public static String ACTION_PLAYPAUSE="ActionReveicerPlayPause";
public static String ACTION_UPDATEINFO="ActionReceiverUpdateInfo";
public static String ACTION_REFRESH="ActionReceiverRefresh";
public static String ACTION_SHOWMAIN="ActionShowMain";
@Override
public void onReceive(Context ctxt, Intent intent) {
if (intent.getAction().equals(ACTION_REFRESH)) {
Log.d("RPod_Widget", ACTION_REFRESH);
} else if (intent.getAction().equals(ACTION_UPDATEINFO)) {
Log.d("RPod_Widget", ACTION_UPDATEINFO);
} else if (intent.getAction().equals(ACTION_PLAYPAUSE)) {
Log.d("RPod_Widget", ACTION_PLAYPAUSE);
} else if (intent.getAction().equals(ACTION_SHOWMAIN)) {
Log.d("RPod_Widget", ACTION_SHOWMAIN);
} else {
Log.d("RPod_Widget", intent.getAction());
super.onReceive(ctxt, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d("RPod_Widget","Action: onUpdate");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context, MainActivity.class);
active.setAction(ACTION_SHOWMAIN);
PendingIntent actionPendingIntent = PendingIntent.getActivity(context, 234, active, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetLayout, actionPendingIntent);
Intent active2 = new Intent(context, AppWidgetProvider.class);
active2.setAction(ACTION_PLAYPAUSE);
PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 235, active2, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetPlayButton, actionPendingIntent2);
Intent active3 = new Intent(context, AppWidgetProvider.class);
active3.setAction(ACTION_UPDATEINFO);
PendingIntent actionPendingIntent3 = PendingIntent.getBroadcast(context, 236, active3, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetReloadButton, actionPendingIntent3);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
當我點擊該窗口小部件佈局的主要活動得到提高,但按鍵完全忽略。我點擊它們但沒有效果。沒有來自onReceive的消息正在Logcat上顯示,僅此:
07-05 15:41:41.675: DEBUG/RPod_Widget(4144): Action: onUpdate
07-05 15:44:39.261: DEBUG/RPod_Widget(4198): android.appwidget.action.APPWIDGET_UPDATE
07-05 15:44:39.261: DEBUG/RPod_Widget(4198): Action: onUpdate
07-05 15:49:48.078: DEBUG/RPod_Widget(4285): android.appwidget.action.APPWIDGET_UPDATE
07-05 15:49:48.078: DEBUG/RPod_Widget(4285): Action: onUpdate
有些建議嗎?謝謝!
我試圖改變以下你的建議的所有字符串,但問題仍然存在。此外,您的建議不符合http://stackoverflow.com/questions/2471875/processing-more-than-one-button-click-at-android-widget上提到的所有提議。不管怎麼說,還是要謝謝你。 – 2012-07-05 14:22:43