Android开发之AppWidget详解

 更新时间:2021年04月09日 17:19:43   作者:Bue_Sky  
这篇文章主要介绍了Android开发之AppWidget详解,想了解桌面程序AppWidget的同学可以参考下

Android通知系统是它的一大特色,而其中,AppWidget是其中一个亮点。在开发应用的中,很多时候可以为其添加一个AppWidget显示在桌面中,及时方便的与用户进行

交互。这里就简单的熟悉一下开发一个AppWidget的流程吧。

想要在应用中创建一个AppWidget,至少需要以下几样东西:

  1. 需要创建一个AppWidgetProviderInfo,来描述AppWidget的元数据。
  2. 需要实现一个自己的AppWidgetProvider对AppWidget进行更新等操作。
  3. 需要布局文件来描述AppWidget的布局。

那么,下面就开始创建一个AppWidget吧。

一、在AndroidManifest.xml中声明一个AppWidget

首先我们需要在AndroidManifest.xml中声明AppWidgetProvider。格式如下:

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

可以看出AppWidgetProvider实际上就是一个BroadcastReceiver,它接收特定的Broadcast。<meta-data>标签描述了AppWidget所使用的元数据,android:resource则声明了定义元数据的xml文件的位置。

二、添加AppWidgetProviderInfo元数据

AppWidgetProviderInfo描述了AppWidget的本质特性,例如,AppWidget更新的周期,最小的宽度、长度,所使用的布局文件是什么,以及添加AppWidget需要启动的

configuration Activity等。我们需要在XML中来定义AppWidgetProviderInfo对象,这个XML文件应该保存在res/xml文件夹下。下面是一个范例:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    android:minWidth="294dp"  
    android:minHeight="72dp"  
    android:updatePeriodMillis="86400000"  
    android:previewImage="@drawable/preview"  
    android:initialLayout="@layout/example_appwidget"  
    android:configure="com.example.android.MyAppWidgetConfigure"   
    android:resizeMode="horizontal|vertical">  
</appwidget-provider>  

<appwidget-provider>需要使用这个标签来定义AppWidgetProviderInfo。下面对范例中使用到的属性做下说明。

minWidthminHeight定义了AppWidget需要占据的最小的空间。

updatePeriodMillis定义了大概多久AppWidget需要更新一次,这里定义的只是一个大概的时间,系统不能做出精确的保证。

previewImage定义了在用户选择AppWidget时做现实的图标。

initialLayout定义了AppWidget所使用的布局文件。

configure定义了AppWidget在添加的时候需要启动的configuration Activity 用于执行配置的工作。

resizeMode定义了缩放模式。

三、创建AppWidget所使用的布局文件

在创建AppWidget时必须创建一个布局文件,为其提供布局描述。AppWidget创建视图时,需要根据RemoteViews来创建。而出于效率等因素的考虑,很多控件在

RemoteViews中是被支持的。以下列出能在RemoteViews中使用的UI控件:

layout : FrameLayout , LinearLayout , RelativeLayout

widget : AnalogClock , Button , Chronometer , ImageButton , ImageView , ProgressBar , TextView , ViewFlipper , ListView , GridView , StackView , AdapterViewFlipper

四、创建一个AppWidgetProvider的子类

前面提到过AppWidgetProvider就是一个BroadcastReceiver。对,它其实确实是继承自BroadcastReceiver,只是它为了更加方便的处理AppWidget的广播进行了封装。

AppWidgetProvider在接收到AppWidget的广播的时候,会根据类型分别触发以下几个方法:

onUpdate() : 当AppWidget需要更新时,会触发这个方法,我们需要重写这个方法,在里面实现更新的操作。如果没有定义configuration Activity,那么在添加一个AppWidget

时,也会触发此方法。

onDelete(Context , int[] ):当AppWidget从AppWidgetHost中删除时,会触发此方法。

onEnabled(Context ):如果为一个应用添加了多个AppWidget,只有在第一个AppWidget被添加时,此方法才会被调用。

onDisabled(Context ):当一个应用的最后一个AppWidget从AppWidgetHost中删除时,会触发此方法。

onReceive(Context , Intent ):这实际上就是BroadcastReceiver中的方法,当任何一个Broadcast被接收到时,会调用此方法,并且会在以上回调方法之前被调用。

五、创建一个ConfigurationActivity(可选)

如果需要AppWidget添加的时候做一些配置工作,就可以使用Configuration Activity。要使用ConfigurationActivity首先需要像普通的Activity一样在AndroidManifest.xml中

进行声明:

<activity android:name=".ExampleAppWidgetConfigure">  
    <intent-filter>  
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>  
    </intent-filter>  
</activity>  

只是这里需要添加action类型为android.appwidget.action.APPWIDGET_CONFIGURE的intent-filter。然后,需要在AppWidgetProviderInfo中进行声明:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    ...  
    android:configure="com.example.android.ExampleAppWidgetConfigure"   
    ... >  
</appwidget-provider>  

最后,当然是需要创建Activity了,在Configuration Activity中,需要执行一些必要的操作:

1、获取AppWidget ID

Intent intent = getIntent();  
Bundle extras = intent.getExtras();  
if (extras != null) {  
    mAppWidgetId = extras.getInt(  
            AppWidgetManager.EXTRA_APPWIDGET_ID,   
            AppWidgetManager.INVALID_APPWIDGET_ID);  
}  

2、进行必要的配置操作,获取AppWidgetManager实例、更新RemoteViews

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
 
RemoteViews views = new RemoteViews(context.getPackageName(),  
R.layout.example_appwidget);  
appWidgetManager.updateAppWidget(mAppWidgetId, views);  

3、设置Activity result,并且返回一个Intent。

Intent resultValue = new Intent();  
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);  
setResult(RESULT_OK, resultValue);  
finish();  

这样一个就创建好了一个Configuration Activity了。

注意android8.0以后无法收到发给自己的AppWidgetProvider,需要添加

intent.setComponent(new ComponentName(context,CacheProvider.class));
Intent intent = new Intent();
intent.setAction(ACTION_CACHE_CLEAN);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setComponent(new ComponentName(context,CacheProvider.class));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tv_clean, pendingIntent);

执行完上面的步骤,就已经创建了一个可以在桌面进行显示的AppWidget了。

以上就是Android开发之AppWidget详解的详细内容,更多关于Android AppWidget详解的资料请关注脚本之家其它相关文章!

相关文章

  • Android实现左滑删除列表功能

    Android实现左滑删除列表功能

    这篇文章主要为大家详细介绍了Android自定义左滑删除列表功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android采用GET方法进行网络传值

    Android采用GET方法进行网络传值

    这篇文章主要为大家详细介绍了Android采用GET方法进行网络传值的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android中AsyncTask的入门使用学习指南

    Android中AsyncTask的入门使用学习指南

    AsyncTask异步任务,用于执行耗时任务并在UI线程中更新结果。下面这篇文章主要给大家介绍了关于Android中AsyncTask入门使用的相关资料,需要的朋友可以参考下
    2019-02-02
  • 详解Android自定义控件属性

    详解Android自定义控件属性

    这篇文章主要为大家详细介绍了Android自定义控件属性,需要的朋友可以参考下
    2016-02-02
  • Retrofit2日志拦截器的使用

    Retrofit2日志拦截器的使用

    这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • Android编程实现动态更新ListView的方法

    Android编程实现动态更新ListView的方法

    这篇文章主要介绍了Android编程实现动态更新ListView的方法,结合实例形式详细分析了ListView的布局及动态更新实现方法,需要的朋友可以参考下
    2016-02-02
  • Android获取蓝牙设备列表的方法

    Android获取蓝牙设备列表的方法

    这篇文章主要为大家详细介绍了Android获取蓝牙设备列表的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Android自定View实现滑动验证效果的代码

    Android自定View实现滑动验证效果的代码

    这篇文章主要介绍了Android自定View实现滑动验证效果,代码分为自定义属性代码和自定义view代码及使用方法,本文给大家介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • Android编程实现的EditText弹出打开和关闭工具类

    Android编程实现的EditText弹出打开和关闭工具类

    这篇文章主要介绍了Android编程实现的EditText弹出打开和关闭工具类,涉及Android输入框EditText弹出打开和关闭功能简单实现技巧,需要的朋友可以参考下
    2018-02-02
  • MobLink Android 快速集成指南

    MobLink Android 快速集成指南

    这篇文章主要为大家介绍了MobLink Android 快速集成指南,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10

最新评论