新闻中心

EEPW首页>嵌入式系统>设计应用> 介绍桌面widgets和AppWidget框架(译)

介绍桌面widgets和AppWidget框架(译)

作者: 时间:2016-10-08 来源:网络 收藏

updateViews.setTextViewText(R.id.word_type, matcher.group(2));

updateViews.setTextViewText(R.id.definition, matcher.group(3).trim());

// When user clicks on widget, launch to Wiktionary definition page

String definePage = res.getString(R.string.template_define_url,

Uri.encode(wordTitle));

Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));

PendingIntent pendingIntent = PendingIntent.getActivity(context,

0 /* no requestCode */, defineIntent, 0 /* no flags */);

updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

} else {

// Didn’t find word of day, so show error message

updateViews = new RemoteViews(context.getPackageName(),

R.layout.widget_message);

CharSequence errorMessage = context.getText(R.string.widget_error);

updateViews.setTextViewText(R.id.message, errorMessage);

}

return updateViews;

}

@Override public IBinder onBind(Intent intent) {

// We don’t need to bind to this service

return null;

}

}

}

到这里,你已经完成了一个简单的widget,它将显示Wiktionary “Word of the day.”。当一个更新被请求时,我们读在线API将最新的数据push到widget上。AppWidget framework会按我们的需要自动更新,例如当一个新的widget添加时,或者新的一天加载新的Word of the day.”。

最后,这里给出一些建议。Widgets推荐被设计成longer-term的内容,不应该经常的被更新。超过每小时的频繁更新会快速消耗掉电量和带宽。建议尽可能的不要频繁更新,或者让你的用户自定义一个更新周期。例如有些人可能想stock ticker每15分钟更新一次,或者可能是一天更新四次。我将在我的另一篇文章giving at Google I/O讨论节省电量的一些额外的策略。

最后要提的一件比较酷的事是AppWidget framework对方向并不关心(is abstracted in both directions).这意味着你可以在两个home screen都可以包含widgets。你的widgets可以被添加到任何一个支持AppWidgetframework的home screen上。

我们已经开发了几个自己的widgets,例如Calendar和Music widgets,但是我们更希望看到你开发的widgets.


上一页 1 2 下一页

关键词:

评论


相关推荐

技术专区

关闭