(android) 添加一个小部件

发布于 2024-12-10 16:33:23 字数 676 浏览 0 评论 0原文

好吧,我创建了这个小部件,

public class Widget extends AppWidgetProvider{
private SimpleDateFormat formatter = new SimpleDateFormat (
    "EEEEEEEEE\nd MMM yyyy");

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    String now = formatter.format(new Date());

    RemoteViews updateViews = new RemoteViews (
            context.getPackageName() , R.layout.widget );
    updateViews.setTextViewText(R.id.text, now);
    appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    }
}

它只显示日期,但我不知道如何将此类添加到我的主程序中,并最终在主屏幕上显示该小部件。你能帮助我吗?

Well, i have created this widget

public class Widget extends AppWidgetProvider{
private SimpleDateFormat formatter = new SimpleDateFormat (
    "EEEEEEEEE\nd MMM yyyy");

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    String now = formatter.format(new Date());

    RemoteViews updateViews = new RemoteViews (
            context.getPackageName() , R.layout.widget );
    updateViews.setTextViewText(R.id.text, now);
    appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    }
}

It just shows the date, but i dont know how to add this class to my main program and finally show the widget on the home screen. Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚 2024-12-17 16:33:23

这是一个链接,指向类似的小部件,该小部件为 XMas.you 提供了剩余的天数可以根据您的需要进行定制。

当用户从小部件列表中选择它时,小部件就会显示。所以我认为,你只需要担心哪个小部件将显示的数据。其余的将由用户决定是否将该小部件保留在主屏幕上。

Here is a link to similar widget which gives days left to XMas.you can customize it to your need.

And widget would be displayed when user choose it from widget lists. So i think,you just have to worry for the data which widget would display.rest would be upto user whether to keep that widget on homescreen or not.

_畞蕅 2024-12-17 16:33:23

Widget 基本上是一个广播接收器。如果您已经有一个应用程序,请将此小部件(广播接收器)添加到其清单文件中。

这是一个链接 - http://developer.android.com/guide/topics/ appwidgets/index.html

Widget is basically a broadcast receiver. if you already have an application, then add this widget(broadcast receiver) to its manifest file.

Here is a link - http://developer.android.com/guide/topics/appwidgets/index.html

〆凄凉。 2024-12-17 16:33:23

剩余天数 Widget ## public class WidgetProvider extends AppWidgetProvider {

private PendingIntent alarmIntent;
private final long alarmInterval = AlarmManager.INTERVAL_HOUR;
public final String REFRESH_ACTION = "DAYSLEFT_WIDGET_REFRESH";

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (REFRESH_ACTION.equals(intent.getAction())) {
        updateAllWidgets(context);
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager widgetManager,
        int[] ids) {
    updateAllWidgets(context, widgetManager, ids);
    setupAlarm(context);
}

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

public static void updateAppWidget(Context context,
        AppWidgetManager appWidgetManager, int widgetId) {
    new WidgetRefresher(context, appWidgetManager).refresh(widgetId);
}

private void updateAllWidgets(Context context) {
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

    final String className = WidgetProvider.class.getName();
    ComponentName componentName = new ComponentName(
            context.getPackageName(), className);

    final int[] widgetIds = widgetManager.getAppWidgetIds(componentName);
    updateAllWidgets(context, widgetManager, widgetIds);
}

private void updateAllWidgets(Context context,
        AppWidgetManager widgetManager, final int[] ids) {
    WidgetRefresher refresher = new WidgetRefresher(context, widgetManager);
    for (int i = 0; i < ids.length; i++) {
        refresher.refresh(ids[i]);
    }
}

private void setupAlarm(Context context) {
    if (alarmIntent == null) {
        final Intent intent = new Intent(REFRESH_ACTION);

        alarmIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final AlarmManager alarms = getAlarmManager(context);
        alarms.setInexactRepeating(AlarmManager.RTC, new Date().getTime(),
                alarmInterval, alarmIntent);

    }
}

private void teardownAlarm(Context context) {
    if (alarmIntent != null) {
        getAlarmManager(context).cancel(alarmIntent);
    }
}

private final AlarmManager getAlarmManager(Context context) {
    return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
}

private static class WidgetRefresher {
    Context context;
    AppWidgetManager appWidgetManager;

    public WidgetRefresher(Context context,
            AppWidgetManager appWidgetManager) {
        this.context = context;
        this.appWidgetManager = appWidgetManager;
    }

    public void refresh(int widgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        WidgetConfiguration config = getConfig(widgetId);

        SimpleDate eventDate = config.getSimpleDate();
        if (eventDate != null) {
            updateTextWithDate(views, eventDate);
        } else {
            updateTextWithoutDate(views);
        }

        updateTitle(views, config.getTitle());
        setupClickIntent(views, widgetId);

        appWidgetManager.updateAppWidget(widgetId, views);
    }

    private void updateTextWithDate(RemoteViews views, SimpleDate date) {
        String daysLeft = Integer.toString(date.getDaysLeft());
        views.setTextViewText(R.id.eventDate, date.toString());
        views.setTextViewText(R.id.daysLeft, daysLeft);
    }

    private void updateTextWithoutDate(RemoteViews views) {
        views.setTextViewText(R.id.eventDate, "");
        views.setTextViewText(R.id.daysLeft, "");
    }

    private void updateTitle(RemoteViews views, String title) {
        if (title == null) {
            title = context.getString(R.string.no_title);
        }
        views.setTextViewText(R.id.eventTitle, title);
    }

    private WidgetConfiguration getConfig(int widgetId) {
        return new WidgetConfiguration(context, widgetId);
    }

    private void setupClickIntent(RemoteViews views, int widgetId) {
        Intent intent = new Intent(context, ConfigureActivity.class);

        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        views.setOnClickPendingIntent(R.id.eventTitle, pendingIntent);
    }
}

}
https://play.google.com/store/apps/details ?id=com.Free.daysleft

Days Left Widget ## public class WidgetProvider extends AppWidgetProvider {

private PendingIntent alarmIntent;
private final long alarmInterval = AlarmManager.INTERVAL_HOUR;
public final String REFRESH_ACTION = "DAYSLEFT_WIDGET_REFRESH";

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (REFRESH_ACTION.equals(intent.getAction())) {
        updateAllWidgets(context);
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager widgetManager,
        int[] ids) {
    updateAllWidgets(context, widgetManager, ids);
    setupAlarm(context);
}

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

public static void updateAppWidget(Context context,
        AppWidgetManager appWidgetManager, int widgetId) {
    new WidgetRefresher(context, appWidgetManager).refresh(widgetId);
}

private void updateAllWidgets(Context context) {
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

    final String className = WidgetProvider.class.getName();
    ComponentName componentName = new ComponentName(
            context.getPackageName(), className);

    final int[] widgetIds = widgetManager.getAppWidgetIds(componentName);
    updateAllWidgets(context, widgetManager, widgetIds);
}

private void updateAllWidgets(Context context,
        AppWidgetManager widgetManager, final int[] ids) {
    WidgetRefresher refresher = new WidgetRefresher(context, widgetManager);
    for (int i = 0; i < ids.length; i++) {
        refresher.refresh(ids[i]);
    }
}

private void setupAlarm(Context context) {
    if (alarmIntent == null) {
        final Intent intent = new Intent(REFRESH_ACTION);

        alarmIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final AlarmManager alarms = getAlarmManager(context);
        alarms.setInexactRepeating(AlarmManager.RTC, new Date().getTime(),
                alarmInterval, alarmIntent);

    }
}

private void teardownAlarm(Context context) {
    if (alarmIntent != null) {
        getAlarmManager(context).cancel(alarmIntent);
    }
}

private final AlarmManager getAlarmManager(Context context) {
    return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
}

private static class WidgetRefresher {
    Context context;
    AppWidgetManager appWidgetManager;

    public WidgetRefresher(Context context,
            AppWidgetManager appWidgetManager) {
        this.context = context;
        this.appWidgetManager = appWidgetManager;
    }

    public void refresh(int widgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        WidgetConfiguration config = getConfig(widgetId);

        SimpleDate eventDate = config.getSimpleDate();
        if (eventDate != null) {
            updateTextWithDate(views, eventDate);
        } else {
            updateTextWithoutDate(views);
        }

        updateTitle(views, config.getTitle());
        setupClickIntent(views, widgetId);

        appWidgetManager.updateAppWidget(widgetId, views);
    }

    private void updateTextWithDate(RemoteViews views, SimpleDate date) {
        String daysLeft = Integer.toString(date.getDaysLeft());
        views.setTextViewText(R.id.eventDate, date.toString());
        views.setTextViewText(R.id.daysLeft, daysLeft);
    }

    private void updateTextWithoutDate(RemoteViews views) {
        views.setTextViewText(R.id.eventDate, "");
        views.setTextViewText(R.id.daysLeft, "");
    }

    private void updateTitle(RemoteViews views, String title) {
        if (title == null) {
            title = context.getString(R.string.no_title);
        }
        views.setTextViewText(R.id.eventTitle, title);
    }

    private WidgetConfiguration getConfig(int widgetId) {
        return new WidgetConfiguration(context, widgetId);
    }

    private void setupClickIntent(RemoteViews views, int widgetId) {
        Intent intent = new Intent(context, ConfigureActivity.class);

        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        views.setOnClickPendingIntent(R.id.eventTitle, pendingIntent);
    }
}

}
https://play.google.com/store/apps/details?id=com.Free.daysleft

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文