Обновить виджет приложения по клику на андроид - PullRequest
4 голосов
/ 26 декабря 2011

Я работаю над Android Widget, мой виджет похож на следующую диаграмму, [предыдущая] имя события [следующая]

prev, далее - кнопки на виджете. Поэтому я хочу обновить имя события, нажав кнопку next и prev. При запуске службы я загрузил данные и сохранил их в списке массивов. Теперь я хочу перебрать список массивов, нажимая кнопки «Далее» и «Предыдущая» на виджете.

Так, как я могу достигнуть этого. Пожалуйста, помогите мне с правильным решением. Вот мой макет xml

<LinearLayout
    android:id="@+id/lytWidget2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnPreviousView"
        android:layout_width="43dp"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center"
        android:background="@drawable/ic_leftarrow"
        android:padding="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="52dp"
        android:layout_weight="0.97"
        android:text="Event Name" />

    <Button
        android:id="@+id/btnNextView"
        android:layout_width="43dp"
        android:layout_height="fill_parent"
        android:layout_gravity="right|center_vertical"
        android:background="@drawable/ic_rightarrow"
        android:padding="5dip" />
</LinearLayout>

Ответы [ 3 ]

10 голосов
/ 26 декабря 2011

Итак, просто обрабатывайте события нажатия ваших кнопок в классе AppWidgetProvider следующим образом.Посмотрите этот хороший урок о том, как создать простой виджет.Также добавлены обработчики щелчков.

Пример кода

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

      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
      // find your TextView here by id here and update it.

      Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show(); 
 } 
0 голосов
/ 26 ноября 2018

NewAppWidget.java

package com.example.android.appwidgetsample;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

import java.text.DateFormat;
import java.util.Date;

/**
 * App widget provider class, to handle update broadcast intents and updates
 * for the app widget.
 */
public class NewAppWidget extends AppWidgetProvider {

    // Name of shared preferences file & key
    private static final String SHARED_PREF_FILE =
            "com.example.android.appwidgetsample";
    private static final String COUNT_KEY = "count";

    /**
     * Update a single app widget.  This is a helper method for the standard
     * onUpdate() callback that handles one widget update at a time.
     *
     * @param context          The application context.
     * @param appWidgetManager The app widget manager.
     * @param appWidgetId      The current app widget id.
     */
    private void updateAppWidget(Context context,
                                AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        // Get the count from prefs.
        SharedPreferences prefs =
                context.getSharedPreferences(SHARED_PREF_FILE, 0);
        int count = prefs.getInt(COUNT_KEY + appWidgetId, 0);
        count++;

        // Get the current time.
        String dateString =
                DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());

        // Construct the RemoteViews object.
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.new_app_widget);
        views.setTextViewText(R.id.appwidget_id,
                String.valueOf(appWidgetId));
        views.setTextViewText(R.id.appwidget_update,
                context.getResources().getString(
                    R.string.date_count_format, count, dateString));

        // Save count back to prefs.
        SharedPreferences.Editor prefEditor = prefs.edit();
        prefEditor.putInt(COUNT_KEY + appWidgetId, count);
        prefEditor.apply();

        // Setup update button to send an update request as a pending intent.
        Intent intentUpdate = new Intent(context, NewAppWidget.class);

        // The intent action must be an app widget update.
        intentUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

        // Include the widget ID to be updated as an intent extra.
        int[] idArray = new int[]{appWidgetId};
        intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);

        // Wrap it all in a pending intent to send a broadcast.
        // Use the app widget ID as the request code (third argument) so that
        // each intent is unique.
        PendingIntent pendingUpdate = PendingIntent.getBroadcast(context,
                appWidgetId, intentUpdate, PendingIntent.FLAG_UPDATE_CURRENT);

        // Assign the pending intent to the button onClick handler
        views.setOnClickPendingIntent(R.id.button_update, pendingUpdate);

        // Instruct the widget manager to update the widget.
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    /**
     * Override for onUpdate() method, to handle all widget update requests.
     *
     * @param context          The application context.
     * @param appWidgetManager The app widget manager.
     * @param appWidgetIds     An array of the app widget IDs.
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them.
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }
}

NewAppWidget.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#09C"
                android:padding="@dimen/widget_margin" >

    <!-- Panel for Widget ID -->
    <LinearLayout
        android:id="@+id/section_id"
        style="@style/AppWidgetSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/appwidget_id_label"
            style="@style/AppWidgetLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="@string/widget_id_label"/>

        <TextView
            android:id="@+id/appwidget_id"
            style="@style/AppWidgetText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="end"
            android:text="XX"/>
    </LinearLayout>

    <!-- Panel for widget update date and number of updates -->
    <LinearLayout
        android:id="@+id/section_update"
        style="@style/AppWidgetSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/section_id"
        android:orientation="vertical">

        <TextView
            android:id="@+id/appwidget_update_label"
            style="@style/AppWidgetLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:text="@string/widget_update_label"/>

        <TextView
            android:id="@+id/appwidget_update"
            style="@style/AppWidgetText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/date_count_format"/>
    </LinearLayout>

    <!-- Update widget button -->
    <Button
        android:id="@+id/button_update"
        style="@style/AppWidgetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/section_update"
        android:layout_centerHorizontal="true"
        android:text="@string/widget_button_update" />
</RelativeLayout>
0 голосов
/ 12 сентября 2018

Во-первых, в ваших WidgetProviderClass вы должны определить свои действия, чтобы вы могли различать их. В этом случае:

private static final String ACTION_UPDATE_CLICK_NEXT = "action.UPDATE_CLICK_NEXT";
private static final String ACTION_UPDATE_CLICK_PREVIOUS = "action.UPDATE_CLICK_PREVIOUS";

Далее, в вашей переопределенной функции onUpdate() вы должны поставить:

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

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

            views.setOnClickPendingIntent(R.id.nextButtonWidget, getPendingSelfIntent(context, ACTION_UPDATE_CLICK_NEXT));
            views.setOnClickPendingIntent(R.id.previousButtonWidget, getPendingSelfIntent(context, ACTION_UPDATE_CLICK_PREVIOUS));

}

Функция для создания намерения, направленного на текущий класс (самому себе):

private PendingIntent getPendingSelfIntent(Context context, String action) {

        Intent intent = new Intent(context, getClass()); // An intent directed at the current class (the "self").
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

При наступлении события onReceive() будет вызвана функция:

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


            if (ACTION_UPDATE_CLICK_NEXT.equals(intent.getAction())) {
                 // if the user clicked next
                }


            else if (ACTION_UPDATE_CLICK_PREVIOUS.equals(intent.getAction())) {
                // if the user clicked previous
        }

}

БОНУС: Если вы хотите вызвать функцию onUpdate(), то эта функция сделает это за вас. Требуется только параметр контекста!

/**
 * A general technique for calling the onUpdate method,
 * requiring only the context parameter.
 *
 * @author John Bentley, based on Android-er code.
 * @see <a href="http://android-er.blogspot.com
 * .au/2010/10/update-widget-in-onreceive-method.html">
 * Android-er > 2010-10-19 > Update Widget in onReceive() method</a>
 */

private void onUpdate(Context context) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName thisAppWidgetComponentName = new ComponentName(context.getPackageName(), getClass().getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName);
    onUpdate(context, appWidgetManager, appWidgetIds);
}
...