[功能]
widget开发和别的应用程序还是有点不同的 因为其使用比较麻烦 所以今天打算建一个widget模版 把一些固定的东西写死 而把具体定制化内容 的地方 告诉大家 以后要使用的话 直接移过去就可以了
[思路]
1. 一个最基本的widget 的内容
2. 扩展内容 包括:
* startActivity(Intent)
* sendBroadcast(Intent)
[代码 步骤]
1. 创建无用的 initialActivity 用途是:运行该应用程序用 设置其属性为:
- <activityandroid:name=".initialActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<actionandroid:name="android.intent.action.MAIN"/>
-
<categoryandroid:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
<activity android:name=".initialActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2. 定义该woidge 所包含的View 及 布局 wlayout.xml :
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="horizontal"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:id="@+id/text"
-
android:textSize="12px"
-
android:textStyle="bold|italic"
-
android:textColor="#008800"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"/>
-
<Button
-
android:id="@+id/next"
-
android:text="Next!"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"/>
-
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:textSize="12px"
android:textStyle="bold|italic"
android:textColor="#008800"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/next"
android:text="Next!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2. 定义该widget所需的属性文件 R.xml.wattra.xml 包括:
写道
1. 占用大小
2. 更新频率
3. 布局文件
- <?xmlversion="1.0"encoding="utf-8"?>
-
<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
-
android:minWidth="246dip"
-
android:minHeight="22dip"
-
android:updatePeriodMillis="1000"
-
android:initialLayout="@layout/wlayout"/>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="246dip"
android:minHeight="22dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/wlayout" />
3. 在提及widget之前 说说widget 的原理
写道
1.其实 widget 也是一种 BroadcastReceiver
2.其action = “android.appwidget.action.APPWIDGET_UPDATE”
写道
所以 我们的工作就是: 实现 AppWidgetProvider 的方法:
void onUpdate(Context context,
AppWidgetManager appWidgetManager,int[] appWidgetIds)
但是 因为 AppWidgetProvider 特殊上下文Context 的缘故 很多方法不可用
所以 我们打算把具体的刷新工作放在Service 然后通过startService 来启动之
写道
本文中 这个Service 就是:WidgetUpdate
该Service 的实现如下:
- publicstaticclassWidgetUpdateextendsService{
- RemoteViewsrview;
-
-
publicvoidonStart(Intentintent,intstartId){
-
-
-
rview=newRemoteViews(getPackageName(),
- R.layout.wlayout);
-
-
- }
-
-
-
-
@Override
-
publicIBinderonBind(Intentarg0){
-
-
returnnull;
- }
-
- }
public static class WidgetUpdate extends Service {
RemoteViews rview;
public void onStart(Intent intent, int startId) {
//to initial RemoteViews instance
rview = new RemoteViews(getPackageName(),
R.layout.wlayout);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
写道
可能很多人对 RemoteView 感动奇怪 问:为什么不要findViewById()
那是因为方法:findViewById() 只在 Activity 中才可见
而 RemoteView 的作用 还是贴一下source 里面的注释把。
/**
* A class that describes a view hierarchy that can be displayed in
* another process. The hierarchy is inflated from a layout resource
* file, and this class provides some basic operations for modifying
* the content of the inflated hierarchy.
*/
public class RemoteViews implements Parcelable, Filter
注意关键字: "in another process"
4. 因为widget 肯定要接收外界的一些信息/数据 而工具就是:BroadcastReceiver 所以需要定义一个BroadcastReceiver 我们定义其为:WidgetInfoListenerHelper
- publicclassWidgetInfoListenerHelperextendsBroadcastReceiver{
- Contextcontext;
-
- WidgetInfoListenerHelperlistener;
-
-
-
publicWidgetInfoListenerHelper(Contextc){
- context=c;
-
-
-
listener=this;
- }
-
-
publicvoidregisterAction(Stringaction){
-
IntentFilterfilter=newIntentFilter();
- filter.addAction(action);
-
- context.registerReceiver(listener,filter);
- }
-
-
@Override
-
publicvoidonReceive(Contextarg0,Intentarg1){
-
- Bundleb=arg1.getExtras();
-
-
if(b.containsKey(HelloHelper.MessageWidgetText)){
- Stringstring=b.getString(HelloHelper.MessageWidgetText);
- updateText(string);
- }
-
-
- }
-
- }
public class WidgetInfoListenerHelper extends BroadcastReceiver {
Context context;
WidgetInfoListenerHelper listener;
//construct
public WidgetInfoListenerHelper(Context c){
context = c;
//to instance it
listener = this;
}
public void registerAction(String action){
IntentFilter filter = new IntentFilter();
filter.addAction(action);
context.registerReceiver(listener,filter);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle b = arg1.getExtras();
if(b.containsKey(HelloHelper.MessageWidgetText)){
String string = b.getString(HelloHelper.MessageWidgetText);
updateText(string);
}
}
}
写道
在WidgetUpdate.onStart() 注册为:
//to register an BroadcastReceiver to listen update message
WidgetInfoListenerHelper helper = new WidgetInfoListenerHelper(this);
helper.registerAction(HelloHelper.BroadcastHelloWidget);
5. 其他:
* startActivity(Intenr)
写道
因为桌面的空间有限 所以一般只会在widget中显示一下粗略信息 当有比较多的信息时 把之放入Activity 而在widget上注册一个单击监听器 来启动目标Activity 来显示详细信息
- publicvoidsetViewActivityClickListener(RemoteViewsremte,intid,Intenti){
-
-
PendingIntentpi=PendingIntent.getActivity(this,1,i,0);
-
- remte.setOnClickPendingIntent(id,pi);
- }
public void setViewActivityClickListener(RemoteViews remte,int id,Intent i){
PendingIntent pi = PendingIntent.getActivity(this,1,i,0);
remte.setOnClickPendingIntent(id, pi);
}
如何使用:
- setViewActivityClickListener(rview,R.id.text,
-
newIntent(this,HelloActivity.class));
- 如此当点击View:R.id.text就会进入目标Activity:HelloActivity
setViewActivityClickListener(rview,R.id.text,
new Intent(this,HelloActivity.class));
如此 当点击View: R.id.text 就会进入目标Activity:HelloActivity
* sendBroadcast(Intent)
写道
上面有提过怎么接受Broadcast 那应该怎么发生Broadcast 呢? 也可以在View上 注册 监听器
- publicvoidsetViewBroadcastClickListener(RemoteViewsremte,intid,Stringfilter){
-
Intenti=newIntent(filter);
-
-
PendingIntentpi=PendingIntent.getBroadcast(this,1,i,0);
-
- remte.setOnClickPendingIntent(id,pi);
- }
public void setViewBroadcastClickListener(RemoteViews remte,int id,String filter){
Intent i = new Intent(filter);
PendingIntent pi = PendingIntent.getBroadcast(this,1,i,0);
remte.setOnClickPendingIntent(id, pi);
}
如何使用:
- setViewBroadcastClickListener(rview,R.id.next,HelloHelper.BroadcastDestTaskNext);
- 如此当点击View:R.id.next就会发生action=HelloHelper.BroadcastDestTaskNext的BroadcastReceiver
setViewBroadcastClickListener(rview,R.id.next,HelloHelper.BroadcastDestTaskNext);
如此 当点击View:R.id.next 就会发生 action=HelloHelper.BroadcastDestTaskNext 的 BroadcastReceiver
5. 最后一个问题:改动widget显示的内容, 比如:
* 改动文字:
- RemoteViews.setTextViewText()
RemoteViews.setTextViewText()
* 改动图片资源
- RemoteViews.setImageViewBitmap()
RemoteViews.setImageViewBitmap()
*
写道
补充 任何View 改动 都必须 刷新widget 才会生效 如何刷新
public void notifyViewChanged(){
// Push update for this widget to the home screen
ComponentName batteryWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(batteryWidget, rview);
}
6. emulator 运行截图:

done!
分享到:
相关推荐
本资源“axure_ipad_widget模板库”专注于为iPad平台提供原型设计的支持,帮助设计师快速构建iOS应用的原型。 在iOS设备中,iPad拥有独特的界面设计规范和用户体验标准,因此针对iPad的原型设计需要考虑其屏幕尺寸...
- **选择合适的项目类型**:在Android Studio中新建项目时,可以选择包含Widget模板的项目类型。 - **添加必要的权限**:根据Widget的功能需求,可能需要在AndroidManifest.xml中添加相应的权限声明。 - **创建...
- **开发自定义Widget模板:** 允许开发者根据特定需求定制自己的Widget模板。 - **主题修改与创建:** 讲述如何修改或创建新的主题来改变Sample Flex Viewer的外观。 - **Widget配置:** 介绍了如何通过配置文件...
- **不使用Widget模板开发Widget**:提供了一种更灵活但技术要求更高的开发方式。 - **开发自定义Widget模板**:指导如何根据具体需求定制模板。 - **修改或创建新主题**:讲解了如何调整Widget的外观和风格。 - **...
文档提供了Widget模板,使得开发者能够快速启动新的Widget项目。通过继承这些模板,开发者可以专注于业务逻辑而不是基础结构。 **6.2 访问地图** Widget可以通过内置的API访问地图数据和服务,如获取地图的当前...
- **开发Widget模板**:高级开发者可以创建自己的Widget模版,以满足特定需求。 - **修改或创建主题**:Flex Viewer支持主题定制,可以更改界面样式,使其符合品牌规范或用户体验需求。 - **Widget配置**:每个...
- 说明了如何利用提供的Widget模板快速启动项目。 - **4.2 获取地图信息** - 指导如何从地图服务获取地理空间数据。 - **4.3 在地图上显示widget数据** - 解释了如何将Widget的数据呈现在地图上。 - **4.4 从...
- **选取一个模板**:选择合适的Widget模板作为起点。 - **设定目标日期**:为倒计时器设置目标日期。 - **测试“倒计时器”**:确保功能正常运作。 - **自定义Widget的外观**:调整Widget的视觉效果。 - **...
此外,社区中还有许多现成的Widget模板和教程,方便初学者入门。 在“网络其他”这个标签下,Yahoo Widget Engine体现了互联网技术与桌面应用的完美融合,它不仅提供了丰富的信息展示方式,也为用户带来了个性化的...
开发Widget时,通常遵循以下步骤:实现IBaseWidget接口,继承BaseWidget类,利用IWidgetTemplate和WidgetTemplate来定义Widget模板。 IBaseWidget接口定义了Widget与WidgetManager之间的通信方法,如设置ID、标题、...
综上所述,Dojo的Widget标签开发是一项核心技能,它涵盖了类的继承、模板设计、事件处理、数据绑定等多个方面。通过熟练掌握这些知识点,开发者能够在Dojo框架下构建出功能强大、易维护的Web应用。
- 可以通过修改或创建新的Widget模板,以及配置Widget的主题来定制化开发。 6. 核心代码理解: - 核心代码涉及事件总线容器的使用、依赖注入的实现、国际化特性及本地化设置。 - 日志和错误处理是核心代码中不可...
- **编写代码:** 根据Widget模板开发新的微件。 **3.2 在Sample Flex Viewer的FB项目之外开发一个widget** 也可以在外部环境中开发微件,然后将其导入Sample Flex Viewer项目中使用。 **3.3 为Flex Builder设置...
标题中的“android axure widget包”指的是一个专门为Android平台设计的Axure组件集合,包含了一系列Android应用中常见的小部件(Widget)模板,如按钮、滑块、开关、列表视图等。这些组件设计得与Android原生UI风格...
首先,我们可以使用软件包提供的artisan命令创建Widget类。 php artisan make:widget RecentNews 此命令生成两个文件: resources/views/widgets/recent_news.blade.php是一个空视图。 如果不需要视图,请添加...
1. Widget模板:ArcGIS提供预定义的Widget模板,可以作为自定义模块的起点。 2. 功能实现:在ActionScript中编写业务逻辑,处理地图事件,如点击、移动等。 3. MXML界面设计:设计用户交互界面,通过绑定数据实现...
这个类通常会继承Dojo的内置Widget基类,如`dijit._WidgetBase`和`dijit._TemplatedMixin`,以获取基本的Widget功能和模板支持。 3. **定义属性(Attributes)**:在Widget类中定义属性,这些属性可以在实例化时...
使用快捷键 Ctrl+N 弹出新建窗体菜单,选择 Widget 模板,点击创建(Create)。然后,从 Qt Designer 主窗体的 Widget Box 里面拖拽控件,例如 Label、SpinBox、HorizontalSpacer、VerticalSpacer,並将其摆放到窗体...
- **模板文件**:可以为Widget定义单独的模板文件,让视图逻辑更加清晰。 总结来说,ThinkPHP的Widget扩展是提高开发效率和代码质量的重要工具。通过合理使用和设计,可以极大地提升项目的可维护性和用户体验。理解...