同一个应用程序中的几个不同的小部件

时间:2015-07-11 21:38:52

标签: java android widget android-widget android-appwidget

我想创建三个不同大小的不同小部件。我没有找到任何教程,除了这个问题how to add multiple widgets in one app?,我就是这样。

这是一个问题,因为稍后你会看到代码的长墙。

当触发APPWIDGET_UPDATE时,我的窗口小部件会以某种方式更新三次。我对其进行了调试,发现重写方法updateRemoteView()从三个子类中启动了三次。如何从一个班级推出它?

我在Manifest中添加了三个不同的接收器:

    <receiver
        android:name=".DaysWidgetSmall"
        android:label="@string/small_widget_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/small_widget_provider" />
    </receiver>
    <receiver
        android:name=".DaysWidgetMedium"
        android:label="@string/medium_widget_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/medium_widget_provider" />
    </receiver>
    <receiver
        android:name=".DaysWidgetLarge"
        android:label="@string/large_widget_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/large_widget_provider" />
    </receiver>

接下来,我创建了一个基类和三个子类:

Base One(部分):

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MILLISECOND, 2000);

    Intent alarmIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    PendingIntent pendingIntent =
            PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 2000, pendingIntent);
}

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

    Bundle extras = intent.getExtras();
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {

        if (extras != null) {
            int[] widgetIds =
                    appWidgetManager.getAppWidgetIds(
                            new ComponentName(
                                    context, DaysWidgetSmall.class
                            ));

            for (int appWidgetId : widgetIds) {
                RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
                SharedPreferences sharedPreferences =
                        context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
                updateRemoteView(views, sharedPreferences, appWidgetId);
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }

            widgetIds =
                    appWidgetManager.getAppWidgetIds(
                            new ComponentName(
                                    context, DaysWidgetMedium.class
                            ));

            for (int appWidgetId : widgetIds) {
                RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
                SharedPreferences sharedPreferences =
                        context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
                updateRemoteView(views, sharedPreferences, appWidgetId);
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }

            widgetIds =
                    appWidgetManager.getAppWidgetIds(
                            new ComponentName(
                                    context, DaysWidgetLarge.class
                            ));

            for (int appWidgetId : widgetIds) {
                RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
                SharedPreferences sharedPreferences =
                        context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
                updateRemoteView(views, sharedPreferences, appWidgetId);
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
        }
    }
}

public abstract int getLayout();

public abstract void updateRemoteView(RemoteViews views, SharedPreferences prefs, int id);

这里是三个类之一的简短实现(目前它们通过返回布局而不同,但后来我想扩展它):

public class DaysWidgetSmall extends DaysWidget {

    @Override
    public int getLayout() {
        return R.layout.small_widget;
    }

    @Override
    public void updateRemoteView(RemoteViews views, SharedPreferences prefs, int id) {
        int current = prefs.getInt(PREFS_COUNT_NAME + id, 0);
        prefs.edit().putInt(PREFS_COUNT_NAME + id, ++current).apply();
        views.setTextViewText(R.id.update, current + "");
    }
}

0 个答案:

没有答案