Android开发之App widget用法实例分析

本文实例讲述了Android开发之App widget用法。分享给大家供大家参考,具体如下:

放在桌面上的控件叫做——App widget,例如可以在桌面上添加按钮、图片等等控件,例如桌面播放器的控制面板

AppWidgetProviderInfo对象,它为App Widget提供元数据,包括布局、更新频率等等数据,这个对象不是由我们自己生成的,而是由android自己定义配置完成,这个对象被定义在XML文件中

1、定义AppWidgetProviderInfo对象,在res/xml文件夹当中定义一个名为widget_config.xml文件

<?xml version="1.0" encoding="utf-8"?><appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"  android:minWidth="300dp"  android:minHeight="72dp"  android:updatePeriodMillis="0"  android:initialLayout="@layout/widget_ui"></appwidget-provider>

备注:建立的文件夹名一定是xml,因为只有这样才能被R识别

2、AppWidgetProvider定义了App Widget的基本生命周期

public class MyWidgetProvider extends AppWidgetProvider {  public static int Tag;  public int max;  public int current;  @Override  public void onEnabled(Context context) {    super.onEnabled(context);    System.out.println("第一次被创建时调用这个方法");  }  @Override  public void onDisabled(Context context) {    System.out.println("当最后一个App Widget被删除时调用该方法");  }  @Override  public void onReceive(Context context, Intent intent) {  //调用父类的onReceive方法不能少,否则就无法监听到onUpdate事件了    super.onReceive(context, intent);    System.out.println("接收广播事件");  }  @Override  public void onUpdate(Context context, AppWidgetManager appWidgetManager,      int[] appWidgetIds) {      System.out.println("在到达指定的更新时间之后或者当用户向桌面添加App Widget时调用这个方法");      for(int i = 0; i < appWidgetIds.length; i++){        Intent intent = new Intent(context, HB.class);        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);        //R.layout.widget_ui指的是显示在桌面上的控件布局        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_ui);        //R.id.widgetButton指的是为桌面控件按钮绑定事件        remoteViews.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);        //updateAppWidget方法更新remoteViews        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);      }    }  }  @Override  public void onDeleted(Context context, int[] appWidgetIds){    System.out.println("App Widget被删除时调用这个方法");  }}

3、添加一个布局文件res/layout/widget_ui.xml(在桌面上显示的内容)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">  <Button  android:id="@+id/widget_BT_Up"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="Value++"/>  <Button android:id="@+id/widget_BT_Down"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="Value--"  android:layout_weight="1"/></LinearLayout>

4、在AndroidManifest.xml文件中添加reseiver标签

android:resource=”@xml/widget_config” 指明显示widget_config.xml是appwidget的属性初始化设置android:name=”android.appwidget.action.APPWIDGET_UPDATE” 是android系统提供判定是appwidget的处理方式android:name=”.MyWidgetProvider” 表示处理的类,即继承了AppWidgetProvider类的类

<receiver android:name=".MyWidgetProvider" android:label="myWIdget" android:icon="@drawable/icon">  <intent-filter>    <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>  </intent-filter>  <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config"/></receiver>

备注:App Widget和我们应用程序运行在不同的进程中(App Widget当中的View运行在Home Screen进程中),因此要用到RemoteViews和PendingIntent这两个类来操控桌面的控件

如果你的onDelete、onUpdate等事件没有触发,那么一个重要的原因是,你override了onReceive事件,但是又没有调用super.onReceive(),所以导致这之后的事件都不会触发,AppWidgetProvider的事件处理机制是,onRecieve首先触发,然后由onReceive去触发后续事件。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

接着我们去了遇龙河,那里的水清澈见底,我把脚伸进水里,

Android开发之App widget用法实例分析

相关文章:

你感兴趣的文章:

标签云: