一、AppWidget介绍

1.要在手机生成AppWidget需的东西

(1)AppWidgetProviderInfo

  a).res\xml\example_appwidget_info.xml

  b)a中需要布局文件res\layout\example_appwidget.xml来定义AppWidget的样式

(2)AppWidgetProvider

a)自定义一个类ExampleAppWidgetProvider继承AppWidgetProvider,根据需求重写onXXX()

  b)AppWidgetProvider实质是一个BroadcastReciever,通过接收系统广播来操作AppWidget,所以需要在AndroidManifest.xml定义<receiver>及其<intent-filter>等

2.

二、代码

1.res/xml/example_appwidget_info.xml

 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/example_appwidget"
>
</appwidget-provider>

2.res/layout/example_appwidget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/widgetTextId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="firstWidgetText"
android:background="#000000"
/>
</LinearLayout>

3.ExampleAppWidgetProvider.java

 package mars.appwidget01;

 import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context; public class ExampleAppWidgetProvider extends AppWidgetProvider{ @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println("onupdate");
super.onUpdate(context, appWidgetManager, appWidgetIds);
} @Override
public void onDeleted(Context context, int[] appWidgetIds) {
System.out.println("onDeleted");
super.onDeleted(context, appWidgetIds);
} @Override
public void onDisabled(Context context) {
System.out.println("onDisabled");
super.onDisabled(context);
} @Override
public void onEnabled(Context context) {
System.out.println("onEnabled");
super.onEnabled(context);
} }

4.AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.appwidget01" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ExampleAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" /> </manifest>

ANDROID_MARS学习笔记_S02_005_AppWidget1的更多相关文章

  1. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  2. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. ANDROID_MARS学习笔记_S01_011ProgressBar

    文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...

  4. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  6. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  7. ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

    一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  8. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  9. ANDROID_MARS学习笔记_S01_005CheckBox

    一. 1.checkbox_layout.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...

随机推荐

  1. ASP实现清除HTML标签,清除&nbsp;空格等编码

    '清除HTML格式 Function RemoveHTML(strText) Dim RegEx Set RegEx = New RegExp RegEx.Global = True '清除HTML标 ...

  2. 常用经典SQL语句大全(基础)

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sqlserver --- 创 ...

  3. iOS NSMutableArray替换某个元素

    A * a1 = [A new]; A * a2 = [A new]; A * a3 = [A new]; A * a4 = [A new]; NSMutableArray *arr = [[NSMu ...

  4. iOS 的一点理解(一) 代理delegate

    做了一年的iOS,想记录自己对知识点的一点理解. 第一篇,想记录一下iOS中delegate(委托,也有人称作代理)的理解吧. 故名思议,delegate就是代理的含义, 一件事情自己不方便做,然后交 ...

  5. ugui自制摇杆。

    珍爱生命,远离插件. 以上8个字,好好理解. 反正我是这么觉得. 我说的是unity,不是魔兽世界. 总有一天,我会一句一句写出属于自己的东西. 可以开始主题了.         如图所示,建立一个画 ...

  6. CSS高度塌陷

    问题描述:当父元素只包含浮动的元素的时候,且父元素没有设置高度,如果父元素设置了边框border,那么看起来子元素不在父元素之内. 比如这样: html: <div id="paren ...

  7. FreeMarker在JAVA中应用入门

    在项目中通常有生成XML文件发送到另一个系统的需求,简单的办法可以是用一个XML模板,通过Freemarker替换其中的'Mark'(${}),生成最终的XML文件. 下面记录了一下简单的示例步骤: ...

  8. Spring MVC中Ajax实现二级联动

    今天写项目遇到了二级联动,期间遇到点问题,写个博客记录一下. 后台Controller: @RequestMapping("/faultType") @ResponseBody p ...

  9. 重新安装Ubuntu12.04

    重新安装Ubuntu12.04 之所以我重新安装Ubuntu,因为我第一次给根目录分配的空间过小,好像是20GB吧~结果编译Android的时候,编译了3个小时候直接中止掉了.郁闷.这个也告诉我们一定 ...

  10. RX学习笔记:正则表达式

    正则表达式 2016-07-03 正则表达式是以字符串模板的形式匹配查找字符的方式. 正则表达式是字符串模板,所以其本身是一个字符串,首尾以反斜杆 / 开始和结束. 在两反斜杆中间的字符串表示要查找的 ...