1.修改布局布局文件 布局中有一个 ImageView,一个 TextView,要求:文字颜色为红色,大小为 20dp,整体背景为透明。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/IMAGE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/apple"/>
<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/IMAGE"
android:layout_gravity="center_vertical"
android:text="Widget"
android:textColor="#ff0000"
android:textSize="20sp" />
2.修改Widget 内容提供者文件 widget_demo_info.xml,不过我这里没有做修改
3.修改 WidgetDemo.java 代码中的 onUpdate 方法,实现重写,为 Widget 添加事件,使得能够返回主页面。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// There may be multiple widgets active, so update all of them
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
其中,updateAppWidget函数为:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
//设置widget的点击跳转对象
Intent clickInt = new Intent(context,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,clickInt,0); //是widget封装好的函数
//设置widget中textview和imageview的点击事件
rv.setOnClickPendingIntent(R.id.IMAGE,pi);
rv.setOnClickPendingIntent(R.id.appwidget_text,pi);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
常用的代码块封装成函数,利用面向对象的特征。
4.实现静态广播部分:
先把原先的静态广播的接收器暂时注释掉
发送广播和原来的操作是一样的,不需要更改,
参考代码中的 STATICATION 为自己设定的广播名称。由于是静态注册所以需要在
AndroidMainfest.xml 中进行注册 ,将原来注册的静态广播注释掉,在widget的java文件对应的receiver的intent-filter增加一条:
<action android:name="android.intent.action.STATICACTION"/>
接受广播的操作,在onRecive函数中,对广播接收到的图片和文字更新到widget中。其中if 条件语句中主要用到的函数为:setTextViewText、setImageViewResource。之后使用 AppWidgetManager 类对 Widget 进行更新:
if (intent.getAction().equals(STATICACTION))
{
//获取数据
text=intent.getStringExtra("name");
Id=intent.getIntExtra("image",0);
//数据传给widget,并分别更新两个控件的内容
remoteViews.setTextViewText(R.id.appwidget_text,text);
remoteViews.setImageViewResource(R.id.IMAGE,Id);
//更新widget
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context.getApplicationContext(),NewAppWidget.class),remoteViews);
}
5.修改动态广播部分:
接收器中的函数操作和静态操作类似,也是要通过remoteview对象:
重写的onReceive函数:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION)){
RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.new_app_widget);
//动态广播情况下获取的数据
String text=intent.getStringExtra("text");
int Id=R.mipmap.dynamic;
//数据传给widget,并分别更新两个控件的内容
remoteViews.setTextViewText(R.id.appwidget_text,text);
remoteViews.setImageViewResource(R.id.IMAGE,Id);
Log.d("Message","action!!!!!!!");
//更新widget
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context.getApplicationContext(),NewAppWidget.class),remoteViews);
//showNotification(text,context);
}
}
三.实验遇到困难以及解决思路
1.在第3步中重写update函数时不知道如何为 Widget 添加事件,使得能够返回主页面
原来remotevies的setOnClickPendingIntent函数是widget封装好的函数,实现了跳转功能。
2.在第4步中在 Widget 类中重写 onReceive 方法,这里需要使用到 RemoteView 以及 Bundle。当接收到对应广播时进行数据处理。搞不清楚这次实验机制应该如何接受广播以及对应的数据处理。
分成动态广播和静态广播2种情况处理,PPT只给出了静态广播的一个范例。必须利用remoteview这种类去对布局文件中的text和image进行设置。
3.静态广播正常工作,动态广播对widget没有影响。
根据TA所指教,动态接收部分必须在DynamicReceiver里面,如果不是得话必须在在manifest里面写动态的intentfilter,也就是说在manifest里面注册了就可以在其他地方接受,不然只能在对应的receiver类里面接受。然后就正常工作了。
4.widget中接收到listview中对应的点击广播之后,应该如何更新widget的图像和文字,对个中细节不太了解。
必须利用remoteview的对象去设置对应的image和text;
5.界面布局过程中字体总是在上方,调试无果。
跑到手机上发现,正常显示。
一、 实验思考及感想
得到的一些新知识如下:
AppWidgetProvider 继承自 BroadcastReceiver,它能接收 widget 相关的广播,例如 widget 的更新、删除、开启和禁用等。
当 widget 更新时onUpdate()被执行。同样,当用户首次添加 widget 时,onUpdate() 也会被调用,这样 widget 就能进行必要的设置工作(如果需要的话) 。但是,如果定义了 widget 的 configure属性(即android:config,后面会介绍),那么当用户首次添加 widget 时,onUpdate()不会被调用;之后更新 widget 时,onUpdate才会被调用。