2013-04-05 134 views
1

ACTION_WIDGET_CLICKED不起作用下面的代码...有人知道这是为什么? MY_WIDGET_UPDATE工作正常。随着接收我敬酒我从来没有看到ACTION_WIDGET_CLICKEDintent.getAction()。似乎我去错了地方,按钮点击不会创建一个广播...不能点击按钮

我也要区分点之间的每个小部件有不同的动作做ACTION_WIDGET_CLICKED。例如:在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> 

部件代码:

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(); 
        } 
     } 

回答

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())); 
    ... 
}