1

在下面的代码ACTION_WIDGET_CLICKED不起作用...有人知道为什么吗? MY_WIDGET_UPDATE工作正常。收到敬酒后,我再也看不到intent.getAction()ACTION_WIDGET_CLICKED。看来我哪里出错了,按钮单击不会创建广播...

我还区分了 istances 之间的点击,小部件的每个 istances 都有不同的操作要做ACTION_WIDGET_CLICKED。示例:在updateAppWidget我必须仅将appWidgetId单击的小部件传递给 Toast

显现:

<?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>

小部件代码:

public class ControlloWidget extends AppWidgetProvider {
    public static String ACTION_WIDGET_CLICKED = "it.fraschi.controllogiardinowg.ControlloWidget.ACTION_WIDGET_CLICKED";
    public static String MY_WIDGET_UPDATE = "it.fraschi.controllogiardinowg.ControlloWidget.MY_WIDGET_UPDATE";
    static String strWidgetText = "";
    public static Boolean choice = false;

        @Override
         public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          super.onReceive(context, intent);
          Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
          if(MY_WIDGET_UPDATE.equals(intent.getAction())){
              Bundle extras = intent.getExtras();
               if(extras!=null) {
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                ComponentName thisAppWidget = new ComponentName(context.getPackageName(), ControlloWidget.class.getName());
                int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

                onUpdate(context, appWidgetManager, appWidgetIds);
               }
              Toast.makeText(context, "AUTOUPDATE", Toast.LENGTH_LONG).show();
          }
          if(ACTION_WIDGET_CLICKED.equals(intent.getAction())){
                Bundle extras = intent.getExtras();
                    /*if(extras!=null) {
                        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                        ComponentName thisAppWidget = new ComponentName(context.getPackageName(), ControlloWidget.class.getName());
                        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);*/
                        choice=true;
                        Toast.makeText(context, "WIDGET PREMUTO", Toast.LENGTH_LONG).show();
                        //onUpdate(context, appWidgetManager, appWidgetIds);

              // }

            }
         }

        @Override
        public void onEnabled(Context context) {
            // TODO Auto-generated method stub
            //super.onEnabled(context);

            Toast.makeText(context, "onEnabled()", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
            // TODO Auto-generated method stub
            //super.onUpdate(context, appWidgetManager, appWidgetIds);

            final int N = appWidgetIds.length;
            for (int i=0; i<N; i++) {
                int appWidgetId = appWidgetIds[i];

                updateAppWidget(context, appWidgetManager, appWidgetId);


            }
             RemoteViews remoteWidget = new RemoteViews(context.getPackageName(),R.layout.widget);


        }



        public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){
                //TestOnClick
               RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
               Intent active2 = new Intent(context, ControlloWidget.class);
               active2.setAction(ACTION_WIDGET_CLICKED);
               PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, appWidgetId, active2, 0);
               remoteViews.setOnClickPendingIntent(R.id.btnEsegui, actionPendingIntent2);

                if (choice){
                    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
                    updateViews.setTextViewText(R.id.btnEsegui, "[" + String.valueOf(appWidgetId) + "]" + strWidgetText + Configurazione.getColor(context, Configurazione.NOME, appWidgetId));
                    appWidgetManager.updateAppWidget(appWidgetId, updateViews);

                    Toast.makeText(context, "ESECUZIONE_ " + String.valueOf(appWidgetId), Toast.LENGTH_LONG).show();

                }else{
                        int number = (new Random().nextInt(100));
                        strWidgetText = Integer.toString(number);

                        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
                        updateViews.setTextViewText(R.id.btnEsegui, "[" + String.valueOf(appWidgetId) + "]" + strWidgetText + Configurazione.getName(context, Configurazione.NOME, appWidgetId));
                        appWidgetManager.updateAppWidget(appWidgetId, updateViews);

                        Log.d("LOG_UPDATE", "Log Update, Widget n."+String.valueOf(appWidgetId));
                        //Toast.makeText(context, "UPDATE_" + String.valueOf(appWidgetId), Toast.LENGTH_LONG).show();
                    }
        }
4

1 回答 1

0

似乎您没有调用where被调用appWidgetManager.updateAppWidget(...)的正确实例。它应该是这样的:updateViewssetOnClickPendingIntent

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()));
  ...
}
于 2013-04-05T18:51:49.217 回答