华域联盟 Andriod Android开发之AppWidget详解

Android开发之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开发之AppWidget详解

转载请保留出处和原文链接:https://www.cnhackhy.com/109217.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android 实现签到足迹功能

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们