AppWidget运用实例

来源:互联网 发布:数据库测试题 编辑:程序博客网 时间:2024/06/11 12:41

今天学习了AppWidget组件,按照网上的教程做了个小测试。
总共有两步:

第一步:注册组件

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

注意:android:resource=”@xml/appwidget”是配置,写在res/xml里面。可以是如下代码:

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/appwidgetlayout"    android:minHeight="72dp"    android:minWidth="294dp"    android:updatePeriodMillis="86400000" ></appwidget-provider>

里面的android:initialLayout表示这个widget的布局,可以是如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/LinearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/txtapp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#ffffff"        android:text="test" >    </TextView>    <Button        android:id="@+id/btnSend"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Send" >    </Button></LinearLayout>

**

第二步:编写自定义类继承自AppWidgetProvider。

**
主要是重写onUpdate和onReceive

@Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager,            int[] appWidgetIds) {        super.onUpdate(context, appWidgetManager, appWidgetIds);        Intent intent = new Intent();        intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");        PendingIntent pendingIntent = PendingIntent.getBroadcast(                context, 0, intent, 0);        RemoteViews remoteViews  = new RemoteViews(                context.getPackageName(),                R.layout.appwidgetlayout);        remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);    }
@Override    public void onReceive(Context context, Intent intent) {        super.onReceive(context, intent);        if (intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")){                             //只能通过远程对象来设置appwidget中的控件状态              RemoteViews remoteViews  = new RemoteViews(context.getPackageName(),R.layout.appwidgetlayout);             //通过远程对象将按钮的文字设置为”hihi”              remoteViews.setTextViewText(R.id.btnSend, "hihi");                 //获得appwidget管理实例,用于管理appwidget以便进行更新操作              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);              //相当于获得所有本程序创建的appwidget              ComponentName componentName = new ComponentName(context,AppWidget.class);              //更新appwidget              appWidgetManager.updateAppWidget(componentName, remoteViews);          }    }

这里主要介绍一下RemoteView
因为appwidget运行的进程和我们创建的应用不在一个进程中,所以必须借助远程对象来操作

想要了解得更详细,可以参考如下文章这里写链接内容

0 0
原创粉丝点击