android小部件(widget)小结

一、为widget添加点击事件
在继承自AppWidgetProvider类的onUpdate方法中添加监听并发送点击事件:

Intent actClick=new Intent("com.zwq.taskMan");
PendingIntent pending= PendingIntent.getBroadcast(context, 0, actClick, 0);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget);
rv.setOnClickPendingIntent(R.id.kill_all_image,
pending);
appWidgetManager.updateAppWidget(appWidgetIds, rv);

还必须在AndroidManifest.xml的receiver中添加相应的动作:

<action android:name="com.zwq.taskMan"></action>

这样就可以在onReceive方法中通过
if ("com.zwq.taskMan".equals(intent.getAction())) {
}过滤到发送的监听事件了,在这儿可以做相应的事件处理。

二、点击widget时启动一个Activity:

// Push newly updated widget to surface
RemoteViews views = PhotoAppWidgetProvider.buildUpdate(this,mAppWidgetId, helper);

// Intent actionIntent=new Intent(INTENT_FLAG);
// PendingIntent pending= PendingIntent.getBroadcast(this, 0, actionIntent, 0);

Intent actionIntent = new Intent(PhotoAppWidgetConfigure.this, Photo.class);
PendingIntent pending = PendingIntent.getActivity(this, 0, actionIntent, 0);

views.setOnClickPendingIntent(R.id.photo, pending);


可以将这个Widget的id传递给该Activity:

Bundle value = new Bundle();
value.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
actionIntent.putExtras(value);


三、一个程序显示多个Widget,如果程序需要显示不同尺寸的widget,可以配置多个widget,具体就是要有多个provider,然后在androidmenifist里面配置多个receive:

<receiver android:name=".PhotoProviderTwo" android:label="two">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info_two"/>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>

<receiver android:name=".PhotoProviderThree" android:label="three">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info_three"/>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>

<receiver android:name=".PhotoProviderFour" android:label="four">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info_four"/>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>


四、更新Widget的图片:
只要在AppWidgetProvider的onReceive方法里面写上更新的代码即可:

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Common.WIDGET_UPDATE_PHOTO)) {
Bundle b=intent.getExtras();
String path=b.getString(Common.CURRENT_PHOTO_PATH);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.photo_frame);
BitmapTool get = new BitmapTool();
Bitmap bitmap = get.getBitmap(path);
bitmap = Bitmap.createBitmap(bitmap);
views.setImageViewBitmap(R.id.widget_photo, bitmap);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context,PhotoProviderThree.class), views);
}
super.onReceive(context, intent);
}

特别注意:
当使用
views.setImageViewResource(R.id.widget_photo, R.drawable.background);

更新时,没什么问题,View里面的图片可以完全更新,这样图片只能来自程序本身自带的。如果需要使用程序以外的图片就要用到:
views.setImageViewBitmap(R.id.widget_photo, bitmap);
这是要注意添加:
bitmap = Bitmap.createBitmap(bitmap);
不然图片是不会更新的。还有就是图片不能太大,如果太大也是不能更新的!
具体问题参考老外的文章:http://code.google.com/p/android/issues/detail?id=8489
http://eyes-free.googlecode.com/svn/trunk/androidsays/src/com/google/marvin/androidsays/WidgetInterface.java

五、Widget不错的参考资料:
http://hi.baidu.com/tdskee/blog/item/3459249b3a6da1a1c9eaf4bc.html
http://hi.baidu.com/tdskee/blog/item/575bbcaf758559d87cd92abe.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值