好吧,我创建了这个小部件
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);
}
}
它只显示日期,但我不知道如何将此类添加到我的主程序,最后在主屏幕上显示小部件。你能救我吗?
答案 0 :(得分:0)
这是一个link到类似的小部件,它给XMas留下了几天的时间。您可以根据需要自定义它。
当用户从小部件列表中选择小部件时,将显示小部件。所以我认为,你只需要担心哪个小部件会显示的数据。对于用户是否将该小部件保留在主屏幕上是最重要的。
答案 1 :(得分:0)
Widget基本上是一个广播接收器。如果您已有应用程序,则将此小部件(广播接收器)添加到其清单文件中。
这是一个链接 - http://developer.android.com/guide/topics/appwidgets/index.html
答案 2 :(得分:0)
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