不可点击的按钮

时间:2013-04-05 06:29:29

标签: android android-widget

在下面的代码ACTION_WIDGET_CLICKED没有工作......有人知道为什么吗? MY_WIDGET_UPDATE工作正常。在接收致敬时,我永远不会看到intent.getAction()的{​​{1}}。看来我错了,按钮点击没有创建广播...

我也要区分不同点之间的点击,小部件的每个点都有不同的行动要对ACTION_WIDGET_CLICKED做。示例:在ACTION_WIDGET_CLICKED我要传递给Toast时,只有小部件的updateAppWidget被点击

清单:

appWidgetId

小工具代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.fraschi.controllogiardinowg"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="it.fraschi.controllogiardinowg.Configurazione"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
            </intent-filter>
        </activity>
        <receiver android:name="ControlloWidget" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="it.fraschi.controllogiardinowg.ControlloWidget.ACTION_WIDGET_CLICKED"/>
                <action android:name="it.fraschi.controllogiardinowg.ControlloWidget.MY_WIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
        </receiver>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您似乎没有在appWidgetManager.updateAppWidget(...)调用updateViews的正确实例上调用setOnClickPendingIntent。它应该是这样的:

final RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);            
updateViews.setTextViewText(...);
...
Intent intent = new Intent(context, MyWidget.class);
intent.setAction(ACTION_WIDGET_CLICKED+String.valueOf(appWidgetId));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
updateViews.setOnClickPendingIntent(R.id.mywidget_id, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);

OnReceive

String action = intent.getAction();
if (action.startsWith(ACTION_WIDGET_CLICKED))
{
  int widgetId = Integer.parseInt(action.substring(ACTION_WIDGET_CLICKED.length()));
  ...
}