Widget onclick无法使用配置活动

时间:2012-10-13 07:12:23

标签: android widget

当我点击小部件中的按钮时,会出现Toast。但是,当我使用配置活动设置按钮的标题然后单击按钮时,Toast不会出现。为什么呢?

public class MyWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

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


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

           Intent intent = new Intent(context, MyWidget.class);
           intent.setAction(ACTION_WIDGET_RECEIVER);
           PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
           remoteViews.setOnClickPendingIntent(R.id.btn_widget, actionPendingIntent);

           appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); //update our widget
       }

      @Override
       public void onDeleted(Context context, int[] appWidgetIds) {
               super.onDeleted(context, appWidgetIds);
       }

       @Override
       public void onDisabled(Context context) {
               super.onDisabled(context);
       }

       @Override
       public void onEnabled(Context context) {
               super.onEnabled(context);
       }

       @Override
       public void onReceive(Context context, Intent intent) {

         if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
         {
            Toast.makeText(context, "You have just clicked the button!", Toast.LENGTH_LONG).show();
         }
         super.onReceive(context, intent);

       }

Appwidget-provider xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:configure="com.bfarago.tutorials.widget.ConfigActivity"
    > 
</appwidget-provider>

清单:

   <activity
            android:name=".ConfigActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>


        <receiver android:name="MyWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.bfarago.tutorials.widget.MyWidget.ACTION_WIDGET_RECEIVER"/>
            </intent-filter>    
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
        </receiver>

配置活动:

public class ConfigActivity extends Activity {

    Button btn;
    EditText et;
    Context c;
    AppWidgetManager awm;
    int awID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.config);

    btn = (Button)findViewById(R.id.btn);
    et = (EditText)findViewById(R.id.et);
    c = ConfigActivity.this;

    //Getting info about the widget that launched this Activity
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    if (extras != null)
    {
        awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    else
    {
        finish();
    }
    awm = AppWidgetManager.getInstance(c);


    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String newcaption = et.getText().toString();

            RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
            views.setTextViewText(R.id.btn_widget, newcaption);
            views.setTextColor(R.id.btn_widget, Color.RED);
            awm.updateAppWidget(awID, views);

            Intent result = new Intent();
            result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
            setResult(RESULT_OK, result);
            finish();
        }
    });

    }
}

如果我从appwidgetprovider xml中删除行android:configure="com.bfarago.tutorials.widget.ConfigActivity",我会看到Toast。

1 个答案:

答案 0 :(得分:0)

您的配置Activity的{​​{1}}来电onClick,但未在其传递的updateAppWidget上调用setOnClickPendingIntent,因此一旦运行,就会不是来自您的小部件的点击事件。修复它与您的RemoteViews方法相同,或者更好的是,重构代码,这样只有一个函数可以创建onUpdate并调用RemoteViews