Android桌面小组件:最简例子
http://blog.csdn.net/winlinvip/article/details/8471212
是Android的实例,今天做了一个最简单的实例,即显示两个文本。
代码下载地址:http://download.csdn.net/detail/winlinvip/4963888
步骤如下:
1. 安装ADT:
下载开发包:http://developer.android.com/sdk/index.html
运行SDK Manager:
下载需要的包:
2.新建Android项目,如下图:
运行eclipse:D:\android\adt\eclipse\eclipse.exe
新建android项目:
选择指定的sdk,输入项目名称:
一路默认就行,在CreateActivity那一步,不用创建activity:
完成后就有一个项目了。
3.添加provider:
打开AndroidManifest.xml,修改application节点添加节点receiver:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".ParentsAppWidgetProvider"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver> </application>其中,name指定了处理的类。
meta-data name必须是android.appwidget.provider,resource指定的是xml配置文件。
4. 添加provider的配置:
新建res/xml文件夹,添加文件appwidget_provider.xml。
内如如下:
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="60dp" android:minHeight="30dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/appwidget_provider" ></appwidget-provider>其中,initialLayout指定了这个provider的初始界面。
5. 添加layout:
新建文件res/layout/appwidget_provider.xml:
内容如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" ><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000" android:text="@string/widget_text"/><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/appwidget_text_white" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffcccccc" android:text="@string/widget_text"/></LinearLayout>其中,添加了两个文本。它的值是在res/values/strings.xml中引用的,所以需要修改这个文件。
6. 修改strings.xml:
添加一行,指定TextView的text文本:
<resources> <string name="app_name">parents</string> <string name="widget_text">WinLin</string></resources>
8.添加处理的类ParentsAppWidgetProvider:
这个类是由AndroidManifest.xml的receiver指定的,前面添加过这个节点,如下图所示:
新建类,需要指定命名空间和父类,如下图所示:
对该类必须添加一个onUpdate方法,在更新时显示layout:
package com.winlin.parents;import com.winlin.parents.R;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.util.Log;import android.widget.RemoteViews;public class ParentsAppWidgetProvider extends AppWidgetProvider { // log tag private static final String TAG = "ParentsAppWidgetProvider"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d(TAG, "onUpdate"); // For each widget that needs an update, get the text that we should display: // - Create a RemoteViews object for it // - Set the text in the RemoteViews object // - Tell the AppWidgetManager to show that views object for the widget. final int N = appWidgetIds.length; for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId); // Construct the RemoteViews object. It takes the package name (in our case, it's our // package, but it needs this because on the other side it's the widget host inflating // the layout from our package). RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); // Tell the widget manager appWidgetManager.updateAppWidget(appWidgetId, views); } }}
编译实例时,总是说找不到com.android.sample.apis.R,这个类是由res自动编译出来的,所以import时需要指定我们自己的命名空间。
例如,我们导入的是自己的命名空间:
这个R的引用:R.layout.appwidget_provider,即为显示这个layout。
9. 编译和导出apk:
使用File->Export...,如下图所示,导出android application:
指定key,然后导出为apk:
安装到手机,即可添加为桌面小组件。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 手把手带你搭建 Elasticsearch 集群
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- Android(安卓)studio gradle 配置阿里镜像
- 用intellij idea开发工具建立android项目时候报错处理
- 时间和日期选择器DatePicker和TimePicker的使用
- android导入项目出现style错误,menu错误
- Android应用开发SharedPreferences存储数据的使用方法
- [Android] 环境配置之Android(安卓)Studio开发NDK