如何处理按钮单击小组件

时间:2012-03-09 11:12:57

标签: android android-widget

我的应用程序和Service上有一个小部件,以便自动更新。

小工具有ImageButton触发手动更新内容。但我无法处理点击事件。

这是我的AppWidgetProvider

public class MyWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        RemoteViews rmViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent active = new Intent(context, MyWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        active.putExtra("msg", "Message for Button 1");
        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, active, 0);

        rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, rmViews );
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
            if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                Log.d(debug_tag, intent.getAction());
            }
    }
}

当我点击按钮时没有任何改变。它甚至没有记录。

Manifest.xml声明:

<receiver android:name=".MyWidget" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <action android:name="com.app.example.MyWidget.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget"/>
</receiver>

怎么了?

顺便说一下,如果我能处理点击,我应该如何手动更新?我是否将它传递给服务部门?

修改:使用getActivity()更改了getBroadcast(),然后点击了一下。但是无法将此消息传递给Service。有什么建议?

2 个答案:

答案 0 :(得分:2)

Update your `AppWidgetProvider`:


public class MyWidget extends AppWidgetProvider {
        public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

        public static int appid[];
        public static RemoteViews rview;
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                int[] appWidgetIds){
            updateWidgetState(context, ""); 
        }
        @Override
        public void onReceive(Context paramContext, Intent paramIntent)
          {
             String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                updateWidgetState(paramContext, str);   
            }
            else
            {
                    if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
                      {
                        int i = paramIntent.getExtras().getInt("appWidgetId", 0);
                        if (i == 0)
                        {

                        }
                        else
                        {
                            int[] arrayOfInt = new int[1];
                            arrayOfInt[0] = i;
                            onDeleted(paramContext, arrayOfInt);
                        }
                      }
              super.onReceive(paramContext, paramIntent);
            }
          }
         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);
          }
         private static RemoteViews buildUpdate(Context paramContext, String paramString)
          {
            // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
            rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
            Intent active = new Intent(paramContext, MyWidget.class);
            active.setAction(ACTION_WIDGET_RECEIVER);
            active.putExtra("msg", "Message for Button 1");
            PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
            rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent);
            if(parmString.equals(ACTION_WIDGET_RECEIVER))
            {
             //your code for update and what you want on button click

            }  
             return rview; 
          }
        @Override
        public void onEnabled(Context context){
            super.onEnabled(context);
           // Toast.makeText(context, "onEnabled()  ", Toast.LENGTH_SHORT).show();
        }
        // Called each time an instance of the App Widget is removed from the host
        @Override
        public void onDeleted(Context context, int [] appWidgetId){
            super.onDeleted(context, appWidgetId);
           // Toast.makeText(context, "onDeleted()  ", Toast.LENGTH_SHORT).show();
        }
        // Called when last instance of App Widget is deleted from the App Widget host.
        @Override
        public void onDisabled(Context context) {
            super.onDisabled(context);
           // Toast.makeText(context, "onDisabled()  ", Toast.LENGTH_SHORT).show();
        }

    }

答案 1 :(得分:-1)

public class MyWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews remoteViews;
    ComponentName watchWidget;

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget_layout);
    watchWidget = new ComponentName(context, MyWidget .class);
    remoteViews.setOnClickPendingIntent(R.id.update, getPendingSelfIntent(context, SYNC_CLICKED));
    appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

    if (SYNC_CLICKED.equals(intent.getAction())) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget_layout);
        watchWidget = new ComponentName(context, MyWidget .class);

        remoteViews.setTextViewText(R.id.update, "TESTING");
        customToast.showToast("manage your click here" ,context);
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);

    }
}

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}