appWidget开发实例

42 篇文章 0 订阅

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值