频道栏目
首页 > 资讯 > Android > 正文

Android的Widget桌面应用学习

16-05-05        来源:[db:作者]  
收藏   我要投稿

一、Android应用的Widget的介绍

介绍:Android应用的Widget是应用程序窗口小部件(Widget)是微型的应用程序视图,它可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新。你可以通过一个App Widget Provider来发布一个Widget。
图片:首先上一张图来给大家看一看效果。
这里写图片描述

二、一些必要的概念介绍

2.1、AppWidgetProvider类

AppWidgetProvider 继承自 BroadcastReceiver,它能接收 widget 相关的广播,例如 widget 的更新、删除、开启和禁用等。

AppWidgetProvider中的广播处理函数如下:
onUpdate()
当 widget 更新时被执行。同样,当用户首次添加 widget 时,onUpdate() 也会被调用,这样 widget 就能进行必要的设置工作(如果需要的话) 。但是,如果定义了 widget 的 configure属性(即android:config,后面会介绍),那么当用户首次添加 widget 时,onUpdate()不会被调用;之后更新 widget 时,onUpdate才会被调用。
onAppWidgetOptionsChanged()
当 widget 被初次添加 或者 当 widget 的大小被改变时,执行onAppWidgetOptionsChanged()。你可以在该函数中,根据 widget 的大小来显示/隐藏某些内容。可以通过 getAppWidgetOptions() 来返回 Bundle 对象以读取 widget 的大小信息,Bundle中包括以下信息:
OPTION_APPWIDGET_MIN_WIDTH – 包含 widget 当前宽度的下限,以dp为单位。
OPTION_APPWIDGET_MIN_HEIGHT – 包含 widget 当前高度的下限,以dp为单位。
OPTION_APPWIDGET_MAX_WIDTH – 包含 widget 当前宽度的上限,以dp为单位。
OPTION_APPWIDGET_MAX_HEIGHT – 包含 widget 当前高度的上限,以dp为单位。
onAppWidgetOptionsChanged() 是 Android 4.1 引入的。
onDeleted(Context, int[])
当 widget 被删除时被触发。
onEnabled(Context)
当第1个 widget 的实例被创建时触发。也就是说,如果用户对同一个 widget 增加了两次(两个实例),那么onEnabled()只会在第一次增加widget时触发。
onDisabled(Context)
当最后1个 widget 的实例被删除时触发。
onReceive(Context, Intent)
接收到任意广播时触发,并且会在上述的方法之前被调用。

总结,AppWidgetProvider 继承于 BroadcastReceiver。实际上,App Widge中的onUpdate()、onEnabled()、onDisabled()等方法都是在 onReceive()中调用的,是onReceive()对特定事情的响应函数。

2.2、AppWidgetProviderInfo

AppWidgetProviderInfo描述一个App Widget元数据,比如App Widget的布局,更新频率,以及AppWidgetProvider 类,这个文件是在res/xml中定义的,后缀为xml。下面是一个事例,还有AppWidgetProviderInfo中的一些常用的属性介绍:

文件名:widget_weather.xml



    

示例说明:
minWidth 和minHeight
它们指定了App Widget布局需要的最小区域。 缺省的App Widgets所在窗口的桌面位置基于有确定高度和宽度的单元网格中。如果App Widget的最小长度或宽度和这些网格单元的尺寸不匹配,那么这个App Widget将上舍入(上舍入即取比该值大的最接近的整数——译者注)到最接近的单元尺寸。
注意:app widget的最小尺寸,不建议比 “4x4” 个单元格要大。关于app widget的尺寸,后面在详细说明。
minResizeWidth 和 minResizeHeight
它们属性指定了 widget 的最小绝对尺寸。也就是说,如果 widget 小于该尺寸,便会因为变得模糊、看不清或不可用。 使用这两个属性,可以允许用户重新调整 widget 的大小,使 widget 的大小可以小于 minWidth 和 minHeight。
注意:
(01) 当 minResizeWidth 的值比 minWidth 大时,minResizeWidth 无效;当 resizeMode 的取值不包括 horizontal 时,minResizeWidth 无效。
(02) 当 minResizeHeight 的值比 minHeight 大时,minResizeHeight 无效;当 resizeMode 的取值不包括 vertical 时,minResizeHeight 无效。
updatePeriodMillis
它定义了 widget 的更新频率。实际的更新时机不一定是精确的按照这个时间发生的。建议更新尽量不要太频繁,最好是低于1小时一次。 或者可以在配置 Activity 里面供用户对更新频率进行配置。 实际上,当updatePeriodMillis的值小于30分钟时,系统会自动将更新频率设为30分钟!关于这部分,后面会详细介绍。
注意: 当更新时机到达时,如果设备正在休眠,那么设备将会被唤醒以执行更新。如果更新频率不超过1小时一次,那么对电池寿命应该不会造成多大的影响。 如果你需要比较频繁的更新,或者你不希望在设备休眠的时候执行更新,那么可以使用基于 alarm 的更新来替代 widget 自身的刷新机制。将 alarm 类型设置为 ELAPSED_REALTIME 或 RTC,将不会唤醒休眠的设备,同时请将 updatePeriodMillis 设为 0。
initialLayout
指向 widget 的布局资源文件
configure
可选属性,定义了 widget 的配置 Activity。如果定义了该项,那么当 widget 创建时,会自动启动该 Activity。
previewImage
指定预览图,该预览图在用户选择 widget 时出现,如果没有提供,则会显示应用的图标。该字段对应在 AndroidManifest.xml 中 receiver 的 android:previewImage 字段。由 Android 3.0 引入。
autoAdvanceViewId
指定一个子view ID,表明该子 view 会自动更新。在 Android 3.0 中引入。
resizeMode
指定了 widget 的调整尺寸的规则。可取的值有: “horizontal”, “vertical”, “none”。”horizontal”意味着widget可以水平拉伸,“vertical”意味着widget可以竖值拉伸,“none”意味着widget不能拉伸;默认值是”none”。Android3.1 引入。
widgetCategory
指定了 widget 能显示的地方:能否显示在 home Screen 或 lock screen 或 两者都可以。它的取值包括:”home_screen” 和 “keyguard”。Android 4.2 引入。
initialKeyguardLayout
指向 widget 位于 lockscreen 中的布局资源文件。Android 4.2 引入。

其中比较重要的是:android:initialLayout=”@layout/widget_weather”这句话,这句话指向的是Widget的布局文件名。

三、Widget使用步骤

1、第一步,设计应用的布局。

就是一个普通的布局文件,不过长宽要设置合适。
如图,我就给一张布局的图片,布局代码自行完成。
这里写图片描述

注意:这里介绍一些关于布局的知识,也是摘自别人的博客,一起学习一下。

1、添加 widget 到lock screen中

默认情况下(即不设置android:widgetCategory属性),Android是将widget添加到 home screen 中。
但在Android 4.2中,若用户希望 widget 可以被添加到lock screen中,可以通过设置 widget 的
android:widgetCategory 属性包含keyguard来完成。

当你把 widget 添加到lock screen中时,你可能对它进行定制化操作,以区别于添加到home screen中的情况。 你能够通过
getAppWidgetOptions() 来进行判断 widget 是被添加到lock screen中,还是home screen中。通过
getApplicationOptions() 获取 Bundle对象,然后读取 Bundle
的OPTION_APPWIDGET_HOST_CATEGORY值:若值为 WIDGET_CATEGORY_HOME_SCREEN, 则表示该
widget 被添加到home screen中; 若值为 WIDGET_CATEGORY_KEYGUARD,则表示该 widget
被添加到lock screen中。

另外,你应该为添加到lock screen中的 widget 单独使用一个layout,可以通过
android:initialKeyguardLayout 来指定。而 widget 添加到home screen中的layout则可以通过
android:initialLayout 来指定。

2、布局
这里写图片描述
如上图所示,典型的App Widget有三个主要组件:一个边界框(A bounding box),一个框架(a Frame),和控件的图形控件(Widget Controls)和其他元素。App Widget并不支持全部的视图窗口,它只是支持视图窗口的一个子集,后面会详细说明支持哪些视图窗口。

要设计美观的App Widget,建议在“边界框和框架之间(Widget Margins)”以及“框架和控件(Widget
Padding)”之间填留有空隙。在Android4.0以上的版本,系统为自动的保留这些空隙。

3、Widget窗口大小

在AppWidgetProviderInfo中已经介绍了,minWidth 和minHeight 用来指定了App
Widget布局需要的最小区域。缺省的App Widgets所在窗口的桌面位置基于有确定高度和宽度的单元网格中。如果App
Widget的最小长度或宽度和这些网格单元的尺寸不匹配,那么这个App
Widget将上舍入(上舍入即取比该值大的最接近的整数——译者注)到最接近的单元尺寸。

例如,很多手机提供4x4网格,平板电脑能提供8x7网格。当widget被添加到时,在满足minWidth和minHeight约束的前提下,它将被占领的最小数目的细胞。
粗略计算minWidth和minHeight,可以参考下面表格:
这里写图片描述
这里写图片描述

App Widget支持的布局和控件

Widget并不支持所有的布局和控件,而仅仅只是支持Android布局和控件的一个子集。
(01) App Widget支持的布局:
  FrameLayout
  LinearLayout
  RelativeLayout
  GridLayout
(02) App Widget支持的控件:
  AnalogClock
  Button
  Chronometer
  ImageButton
  ImageView
  ProgressBar
  TextView
  ViewFlipper
  ListView
  GridView
  StackView
  AdapterViewFlipper
  

2、第二步,编辑AppWidgetProviderInfo对应的资源文件。

文件名:weather_widget_info.xml



    

说明:
(01) android:previewImage,用于指定预览图片。即搜索到widget时,查看到的图片。若没有设置的话,系统为指定一张默认图片。
(02) android:updatePeriodMillis 更新widget的时间间隔(ms)。在实际测试中,发现设置android:updatePeriodMillis的值为5秒时,不管用!跟踪android源代码,发现:
当android:updatePeriodMillis的值小于30分钟时,会被设置为30分钟。也就意味着,即使将它的值设为5秒,实际上也是30分钟之后才更新。因此,我们若向动态的更新widget的某组件,最好通过service、AlarmManager、Timer等方式;本文采用的是service。

3、第三步,编写类文件继承AppWidgetProvider类,重写方法

文件名:AppWidgetProvider.java
public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
    Gson gson;
    RecentWeatherBean recentWeatherBean;
    RetDataBean retDataBean;
    TodayBean todayBean;
    GetDate getDate;
    Context context;
    //保存地址和ID
    SharedPreferences sharedPreferences;
    //启动AppWidgetService服务对应的action
    private final Intent SERVICE_INTENT = new Intent("android.appwidget.action.APP_WIDGET_SERVICE");
    //更新widget的广播对应的action
    private final String ACTION_UPDATE_ALL = "com.llay.widget.UPDATE_ALL";

    //onUpdate()在更新widget时,被执行(当用户点击wiget界面时候可以调用这个方法)
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            //创建一个Intent对象,跳转到app界面
            Intent intent = new Intent(context, GetLocationActivity.class);
            //创建一个PendingIntent,包主intent
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            //获取wiget布局
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_weather);
            remoteViews.setOnClickPendingIntent(R.id.widget_line, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    //当widget被初次添加或者当widget的大小被改变时,被调用
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    //第一个widget被创建时调用
    @Override
    public void onEnabled(Context context) {
        //在第一个widget被创建时,开启服务
        SERVICE_INTENT.setPackage("com.llay.admin.weather");
        context.startService(SERVICE_INTENT);
        super.onEnabled(context);
    }

    //最后一个widget被删除时调用
    @Override
    public void onDisabled(Context context) {
        //在最后一个widget被删除时,终止服务
        SERVICE_INTENT.setPackage("com.llay.admin.weather");
        context.stopService(SERVICE_INTENT);
        super.onDisabled(context);
    }


    //接收广播的回调函数
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        this.context = context;
        if (ACTION_UPDATE_ALL.equals(action)) {
            getDate = new GetDate();
            getDate.execute("http://apis.baidu.com/apistore/weatherservice/recentweathers");
        } else {
            super.onReceive(context, intent);
        }

    }

    //请求数据
    public class GetDate extends AsyncTask {

        @Override
        protected String doInBackground(String... params) {
            sharedPreferences = context.getSharedPreferences("CityAndCode", Context.MODE_PRIVATE);
            String recentJson = Utils.request(params[0], "cityname=" + sharedPreferences.getString("locationCityName", "南通") + "&cityid=" + sharedPreferences.getInt("locationCityCode", 101190501));
            return recentJson;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                gson = new Gson();
                recentWeatherBean = new RecentWeatherBean();
                recentWeatherBean = gson.fromJson(s, RecentWeatherBean.class);
                retDataBean = new RetDataBean();
                retDataBean = recentWeatherBean.getRetData();
                todayBean = new TodayBean();
                todayBean = retDataBean.getToday();
                //更新界面
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_weather);
                switch (todayBean.getType()) {
                    case "晴":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.sunny);
                        break;
                    case "多云":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.cloudy);
                        break;
                    case "阴":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.overcast);
                        break;
                    case "阵雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.shower);
                        break;
                    case "雷阵雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.thundershower);
                        break;
                    case "小雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.light_rain);
                        break;
                    case "中雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.moderate_rain);
                        break;
                    case "大雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.heavy_rain);
                        break;
                    case "暴雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.storm);
                        break;
                    case "大暴雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.heavy_storm);
                        break;
                    case "小到中雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.light_to_moderate_rain);
                        break;
                    case "中到大雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.moderate_to_heavy_rain);
                        break;
                    case "大到暴雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.heavy_to_storm);
                        break;
                    case "暴雨到大暴雨":
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.storm_to_heavy_storm);
                        break;
                    default:
                        remoteViews.setImageViewResource(R.id.wigdet_image, R.mipmap.undefined);
                        break;
                }
                remoteViews.setTextViewText(R.id.widget_type, todayBean.getType());
                remoteViews.setTextViewText(R.id.widget_curtemp, todayBean.getCurTemp());
                remoteViews.setTextViewText(R.id.widget_date, todayBean.getDate());
                remoteViews.setTextViewText(R.id.widget_location, retDataBean.getCity());
                remoteViews.setTextViewText(R.id.widget_week, todayBean.getWeek());
                remoteViews.setTextViewText(R.id.widget_lowtemp, todayBean.getLowtemp());
                remoteViews.setTextViewText(R.id.widget_hightemp, todayBean.getHightemp());
                remoteViews.setTextViewText(R.id.widget_fengxiang, todayBean.getFengxiang());
                remoteViews.setTextViewText(R.id.widget_fengli, todayBean.getFengli());
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                ComponentName componentName = new ComponentName(context, AppWidgetProvider.class);
                appWidgetManager.updateAppWidget(componentName, remoteViews);
            }
        }
    }

}

说明:
(01)当我们创建第一个widget到桌面时,会执行onEnabled()。在onEnabled()中通过 context.startService(SERVICE_INTENT) 启动服务AppWidgetService。服务的作用就是每隔5秒发送一个ACTION_UPDATE_ALL广播给我们,用于更新widget中的数据。仅仅当我们创建第一个widget时才会启动服务,因为onEnabled()只会在第一个widget被创建时才执行。
(02)当我们删除最后一个widget到桌面时,会执行onDisabled()。在onDisabled()中通过 context.stopService(SERVICE_INTENT) 终止服务AppWidgetService。仅仅当我们删除最后一个widget时才会终止服务,因为onDisabled()只会在最后一个widget被删除时才执行。
(03)本工程中,每添加一个widget都会执行onUpdate()。例外情况:在定义android:configure的前提下,第一次添加widget时不会执行onUpdate(),而是执行android:configure中定义的activity。在onUpdate()方法中,有两个类要特别注意:PendingIntent和RemoteViews。PendingIntent用户包裹住一个Intent,然后再调用remoteViews.setOnClickPendingIntent(R.id.widget_line, pendingIntent);方法时,运行Intent。RemoteViews用户获取Widget中的控件,点击Widget中的控件,调用remoteViews.setOnClickPendingIntent(R.id.widget_line, pendingIntent);方法,可以实现一些功能,如点击Widget然后打开应用。
(04)onReceive()中,更新桌面的widget 以及响应按钮点击广播。当收到ACTION_UPDATE_ALL广播时,调用getDate.execute()方法来请求接口,然后再onPostExecute()中更新的widget的数据。

4、第四步,在AndroidManifest.xml中注册AppWidgetProvider

因为AppWidgetProvider原本就是一个BroadCastReceiver,需要在AndroidManifest.xml中注册。


        
            
                
                
            
            
        

5、第五步,其实这里已经结束了,由于该项目是在后台使用Service实施每10S更新一次,所以还要添加Service类

文件名:AppWidgetService.java
public class AppWidgetService extends Service {
    //更新widget的广播对应的action
    private final String ACTION_UPDATE_ALL = "com.llay.widget.UPDATE_ALL";
    //周期性更新widget的周期
    private static final int UPDATE_TIME = 10000;
    //周期性更新widget的线程
    private UpdateThread mUpdateThread;
    private Context mContext;
    //更新周期的计数
    private int count = 0;

    @Override
    public void onCreate() {
        //创建并开启线程UpdateThread
        mUpdateThread = new UpdateThread();
        mUpdateThread.start();
        mContext = this.getApplicationContext();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        //中断线程,即结束线程。
        if (mUpdateThread != null) {
            mUpdateThread.interrupt();
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //服务开始时,即调用startService()时,onStartCommand()被执行。
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    //线程中发送广播
    private class UpdateThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                count = 0;
                //一直在后台中发送广播,action为"com.llay.widget.UPDATE_ALL"
                while (true) {
                    count++;
                    Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
                    mContext.sendBroadcast(updateIntent);
                    Thread.sleep(UPDATE_TIME);
                }
            } catch (InterruptedException e) {
                //将InterruptedException定义在while循环之外,意味着抛出InterruptedException异常时,终止线程。
                e.printStackTrace();
            }
        }
    }
}

在AndroidManifest.xml中注册Service


        
            
        
    

说明:
(01) onCreate() 在创建服务时被执行。它的作用是创建并启动线程UpdateThread()。
(02) onDestroy() 在销毁服务时被执行。它的作用是注销线程UpdateThread()。
(03) 服务UpdateThread 每隔10秒,发送1个广播ACTION_UPDATE_ALL。广播ACTION_UPDATE_ALL在AppWidgetProvider被处理,用来更新widget中的数据。

总结:主要的核心代码

1、当创建一个Widget时,调用onUpdate()方法和onEnabled()方法。
onUpdage()方法,用于用户点击桌面上的Widget,可以开发应用。核心代码如下

for (int i = 0; i < appWidgetIds.length; i++) {
            //创建一个Intent对象,跳转到app界面
            Intent intent = new Intent(context, GetLocationActivity.class);
            //创建一个PendingIntent,包裹住intent
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            //获取wiget布局
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_weather);
            remoteViews.setOnClickPendingIntent(R.id.widget_line, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);

onEnabled()方法,用于新建第一个Widget时,发送Service对应的action,运行AppWidgetService类。核心代码如下

//第一个widget被创建时调用
    @Override
    public void onEnabled(Context context) {
        //在第一个widget被创建时,开启服务
        SERVICE_INTENT.setPackage("com.llay.admin.weather");
        context.startService(SERVICE_INTENT);
        super.onEnabled(context);
    }

2、运行Service,每10S发送BroadCast对应的action,核心代码如下

//线程中发送广播
    private class UpdateThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                count = 0;
                //一直在后台中发送广播,action为"com.llay.widget.UPDATE_ALL"
                while (true) {
                    count++;
                    Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
                    mContext.sendBroadcast(updateIntent);
                    Thread.sleep(UPDATE_TIME);
                }
            } catch (InterruptedException e) {
                //将InterruptedException定义在while循环之外,意味着抛出InterruptedException异常时,终止线程。
                e.printStackTrace();
            }
        }
    }

3、在AppWidgetProvider中的onReceive()方法中接收,BroadCast发送的action,执行相应的更新数据的方法。核心代码如下

//接收广播的回调函数
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        this.context = context;
        if (ACTION_UPDATE_ALL.equals(action)) {
            getDate = new GetDate();
            getDate.execute("http://apis.baidu.com/apistore/weatherservice/recentweathers");
        } else {
            super.onReceive(context, intent);
        }

    }

总结:思路

创建第一个Widget,发送action为”android.appwidget.action.APP_WIDGET_SERVICE”->
AndroidManifest.xml中收到此action,运行AppWidgetService.java文件->
AppWidgetService.java中每10S发送一个广播action为”com.llay.widget.UPDATE_ALL”->
在AppWidgetProvider.java中的onReceive()方法,接收这个广播action,执行更新->
删除最后一个Widget,停止Service

相关TAG标签
上一篇:抓包工具Fiddler
下一篇:Unity3D UNET 模仿局域网游戏(三)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站