Android桌面插件Widgets
来源:互联网 发布:java小轮径公路车 编辑:程序博客网 时间:2024/04/30 23:14
转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/51452565
本文出自【吴孝城的CSDN博客】
Widgets是一个小型的应用程序组件,它可以嵌入到其他应用程序中(如桌面Home),和定期接受更新
官网:https://developer.android.com/intl/zh-cn/guide/topics/appwidgets/index.html
App Widgets的布局是基于RemoteViews,所以并不支持所有的布局或视图控件
RemoteViews对象支持以下布局
FrameLayout
LinearLayout
RelativeLayout
GridLayout
支持以下组件
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
但不支持这些类的后代
在Android Studio中开发Widgets并不难,直接创建就可以
打开Android Studio,右击要创建Widget的包,选择New >> Widget >> App Widget,然后填写文件名,Finish就行
下面说下创建Widgets后几个文件的内容
创建Widget会在AndroidManifest.xml里声明
<receiver android:name=".MyWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_widget_info" /></receiver>
<receiver>需要有一个 android:name=”“属性,来用标明使用AppWidgetProvider的Widget
<intent-filter>元素必须包含一个 <action>元素,和一个android:name属性,这个属性指定了 AppWidgetProvider接收的唯一广播ACTION_APPWIDGET_UPDATE
android:name=”“,指定元数据的名称,用android.appwidget.provider指定是AppWidgetProviderInfo数据
android:resource=”“,指定AppWidgetProviderInfo的资源路径
res\xml文件夹下的AppWidgetProviderInfo文件my_widget_info.xml
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/my_widget" android:initialLayout="@layout/my_widget" android:minHeight="40dp" android:minWidth="40dp" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen"></appwidget-provider>
initialKeyguardLayout:是锁屏界面的布局
minWidth和minHeight:是组件默认的最小宽高
initialLayout:在桌面显示的布局
previewImage:Android3.0收入的当用户选择Widget时出现的预览图,如果没有指定,则显示应用程序的图标
resizeMode:指定Widget调整尺寸的规则,可以是”horizontal”, “vertical”, and “none”
updatePeriodMillis:更新周期,建议最好不要超过一小时一次
widgetCategory:指定Widget可以显示的地方,主屏幕home_screen,锁屏keyguard,锁屏显示的只能在Android5.0以下才有效。
创建一个Widget的初始化布局my_widget.xml
可用的而已和组件上面有提到
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#09C" android:padding="@dimen/widget_margin"> <TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="8dp" android:background="#09C" android:contentDescription="@string/appwidget_text" android:text="My Widget" android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic" /></RelativeLayout>
为Widget添加边框距离
先设置应用程序的targetSdkVersion为14及以上,然后在编辑下面两个文件,其中res/values-v14/dimens.xml没有的话自己新建一个
res/values/dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <!--Refer to App Widget Documentation for margin informationhttp://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout --> <dimen name="widget_margin">8dp</dimen></resources>
res/values-v14/dimens.xml
<?xml version="1.0" encoding="utf-8"?><resources> <!--Refer to App Widget Documentation for margin informationhttp://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout --> <dimen name="widget_margin">0dp</dimen></resources>
下面就可以编写AppWidgetProvider类了
MyWidget.java
package cn.wuxiaocheng.widget;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.RemoteViews;public class MyWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // 根据updatePeriodMillis属性定义的时间进行更新Widget,用户添加Widget时也会调用, // 如果用户定义了configuration Activity,则首次添加Widget时不被onUpdate调用,之后更新才会调用 for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { // 当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为单位 * */ 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时调用 } @Override public void onDisabled(Context context) { // 当最后一个Widget被删除时调用 } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); // 当接收到广播时调用,并且在上述所有方法之前调用, }}
运行后,回到桌面,长按手机屏幕,点击Widgets,找到相应的组件,报到桌面就行,可以移位,长按组件移到上方,可以删除添加的桌面组件
- Android桌面插件Widgets
- 关于android Widgets桌面小插件的开发大概流程
- Android 桌面小部件-App Widgets
- android -- 桌面小控件App Widgets
- 【android开发】桌面小挂件( APP Widgets )
- Android 桌面插件
- Android开发桌面插件
- android 创建桌面快捷方式 、插件
- android 创建桌面快捷方式 、插件
- android 桌面插件 App Widget
- Android桌面插件的开发
- Android桌面插件开发笔记
- 15个令iPhone用户嫉妒的Android widgets 桌面组件
- android 桌面小部件(App Widgets)如何显示自定义的view
- 介绍桌面widgets和AppWidget框架(译)
- 发布Qt Widgets桌面应用程序的方法
- 发布Qt Widgets桌面应用程序的方法
- 【Android】开发桌面小插件(一)
- 移动平台—企业转型需要的核心驱动力
- Spark定制班第7课:Spark Streaming源码解读之JobScheduler内幕实现和深度思考
- 弗洛伊德(Floyd)算法
- Service-四大组件之一
- Loss和神经网络训练
- Android桌面插件Widgets
- Canvas填充样式fillStyle
- Android动画进阶(Interpolator)
- symfony2 学习2 表单创建 使用
- Ant 替换某一文件中的字符串
- pc直接粘贴到div图片
- docker-compose的一个坑
- 使用android-ndk编译SDL
- c++第6次试验