Android - Widget点击监听器不起作用

时间:2016-02-14 11:09:11

标签: android widget onclicklistener android-pendingintent appwidgetprovider

我有2种可能的小部件布局,用户可以通过偏好活动进行更改,我的意思是光明和黑暗。两者看起来都是simillary,onlu颜色不同。事件ID是相同的。我希望能够点击整个小部件,什么将打开活动并通过意图发送necessery数据。我在AppWidgetProvider中的代码根本不起作用。

这是:

private final String ACTION = "CLICK_MY_BUTTON";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    SharedPreferences theme = Data.getPreferences(context, Data.Prefs.THEME);
    boolean dark = theme.getString(Data.widgetsThemeSetings, "1").equals("1");
    int layoutId = dark?R.layout.widget_dark:R.layout.widget_light;
    for (int i:appWidgetIds) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("from", "widget");
        intent.setAction(ACTION);
        int id = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        intent.putExtra("id", id<60?id:id-1);
        intent.putExtra("widgetID", i);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), layoutId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.widget_relative_main, pendingIntent);
        String output = "";
        for (String s:HolidayCalendar.getInstance(context).getTodayTexts()) {
            output += "\n\n" + Data.pointer + " " + s;
        }
        remoteViews.setTextViewText(R.id.widget_text_holiday, output.substring(2));
        appWidgetManager.updateAppWidget(i, remoteViews);
    }
}

和我的一个小部件布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_relative_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rounded_widget_dark"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/widget_text_today"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/today_is"
        android:textColor="#FFF"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/widget_text_holiday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/widget_text_today"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="#FFF" />

</RelativeLayout>

如何让它运作?我不知道可能是不正确的......

1 个答案:

答案 0 :(得分:2)

这是我的工作代码。这将有效。

 for (int i = 0; i < N; i++) {
            int awID = ids[i];

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

                Intent intent = new Intent(this, your intented class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                //views.setTextViewText(R.id.widgetcount, visitstext);
                PendingIntent pendingIntent = PendingIntent.getActivity(this,
                        0, intent, 0);

                views.setOnClickPendingIntent(R.id.widgetimage, pendingIntent);
manager.updateAppWidget(awID, views);
    }