您的位置:首页 > 移动开发

appWidget开发实例

2014-02-16 14:36 267 查看

AppWidget框架类

widget 就是桌面小部件,首先我们来简单的介绍一下AppWidget的框架类。主要包括以下四个。

AppWidgetProvider-----------基于BrodCast事件操作的AppWidget的接口,当appwidget应用update,enable,disable,delete时候,通过他们可以接收到BrodCast事件。其中onUpdate、OnReceive是常用的方法,用于接收更新通知。
AppWidgetProvderInfo---------用于描述Appwidget的元数据对象,如大小,更新频率、初始化界面和AppWidgetProvider类等信息。以xml的形式存在于res/xml的文件夹下面。
AppWidgetManager---------管理appwidget,像appwidgetprovider发送通知。
RemoteViews--------可以在其他应用进程中运行的类,像AppWidgetProvider发送通知。

开发实例

首先新建一个工程,然后在layout文件夹下写好widget的布局文件这里我们命名为appwidgetlayout.xml,就是显示在桌面上的布局。不过要注意的是并不是所有的控件widget都支持。目前支持的有FrameLayout、LinearLayout、RelativeLayout、AnalogClock、Button 、Chronometer 、ImageButton、ImageView 、ProgessBar、TextView、ViewFlipper..首先给出的是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" />

</LinearLayout>


然后就是配置我们的widget在res文件夹下面新建一个xml文件夹在里面新建一个配置文件我们命名为appwidget1.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="200dp"
android:minWidth="200dp"
android:initialLayout="@layout/appwidgetlayout"
android:updatePeriodMillis="888888" >
</appwidget-provider>
最后就是编写代码来控制了。这里我们继承AppWidgetProvider然后我们来看看代码的实现。
public class MyAppWidgetProvider extends AppWidgetProvider {
private final String brodCastString="com.bobo.widgetupdate";
//删除一个widget时候调用
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
//最后一个widget删除的时候调用
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
}
//第一次创建的时候调用
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
/**
* 接收广播事件
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);

if(intent.getAction().equals(brodCastString))
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidgetProvider.class);
remoteViews.setTextViewText(R.id.text, "点击");
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
}
//到达指定更新时间或者用户向桌面添加widget时候调用
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent();
intent.setAction(brodCastString);
PendingIntent intent2 = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
remoteViews.setOnClickPendingIntent(R.id.send, intent2);
remoteViews.setTextViewText(R.id.text, "hehe");
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}


最有需要在我们的androidmainifest文件里面注册我们的广播。
<receiver android:name="com.example.remoteviewdemo.MyWidgetProvider" >
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidgetprovider" />

<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.bobo.widgetupdate" />
</intent-filter>
</receiver>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java andorid appwidget