温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

AppWidget怎么在Android中使用

发布时间:2021-04-12 15:49:57 来源:亿速云 阅读:177 作者:Leah 栏目:开发技术

AppWidget怎么在Android中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一、在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);

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI