一、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

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

2.res/layout/example_appwidget.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/widgetTextId"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="firstWidgetText"
  12. android:background="#000000"
  13. />
  14. </LinearLayout>

3.ExampleAppWidgetProvider.java

  1. package mars.appwidget01;
  2.  
  3. import android.appwidget.AppWidgetManager;
  4. import android.appwidget.AppWidgetProvider;
  5. import android.content.Context;
  6.  
  7. public class ExampleAppWidgetProvider extends AppWidgetProvider{
  8.  
  9. @Override
  10. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  11. int[] appWidgetIds) {
  12. System.out.println("onupdate");
  13. super.onUpdate(context, appWidgetManager, appWidgetIds);
  14. }
  15.  
  16. @Override
  17. public void onDeleted(Context context, int[] appWidgetIds) {
  18. System.out.println("onDeleted");
  19. super.onDeleted(context, appWidgetIds);
  20. }
  21.  
  22. @Override
  23. public void onDisabled(Context context) {
  24. System.out.println("onDisabled");
  25. super.onDisabled(context);
  26. }
  27.  
  28. @Override
  29. public void onEnabled(Context context) {
  30. System.out.println("onEnabled");
  31. super.onEnabled(context);
  32. }
  33.  
  34. }

4.AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="mars.appwidget01" android:versionCode="1" android:versionName="1.0">
  4. <application android:icon="@drawable/icon" android:label="@string/app_name">
  5. <activity android:name=".MainActivity" android:label="@string/app_name">
  6. <intent-filter>
  7. <action android:name="android.intent.action.MAIN" />
  8. <category android:name="android.intent.category.LAUNCHER" />
  9. </intent-filter>
  10. </activity>
  11. <receiver android:name="ExampleAppWidgetProvider">
  12. <intent-filter>
  13. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  14. </intent-filter>
  15. <meta-data android:name="android.appwidget.provider"
  16. android:resource="@xml/example_appwidget_info" />
  17. </receiver>
  18. </application>
  19. <uses-sdk android:minSdkVersion="7" />
  20.  
  21. </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.net 配置二级域名的共享session,并实现sso单点登录

    公司最近做了一个新网站.原先网站的网址是www.xxxx.com.新做的网站要部署到info.xxxx.com.这两个网站要实现单点登录.而新老网站本身机构的原因,对于登录状态的判断,说白了就是对于s ...

  2. 误解了Windows Server AppFabric

    想为自己的流程引擎找一个宿主,选择了几套方案,想先从AppFabric开始,原因主要出于以下几点: 1. 自己用过Windows Service或Form作为一些定时任务等应用的宿主,但苦于学艺不精, ...

  3. CStdioFile 写文件

    前言: 介绍如何使用 CStdioFile 类去写文件. 完整工程代码,点我下载(请注意工程里面需要包含的 <locale.h>头文件 ) 示例: /// My Add // 获取当前路径 ...

  4. Google 常用镜像收集

    1`下面列出几个目前常用的 公共DNS 服务器地址: 名称 DNS 服务器 IP 地址 OpenerDNS 42.120.21.30   百度 DuDNS 180.76.76.76   阿里 AliD ...

  5. CSS3滤镜!!!

    <!DOCTYPE html> <html> <head> <style> img { width: 33%; height: auto; float: ...

  6. Java语言----三种循环语句的区别

    ------- android培训.java培训.期待与您交流! ---------- 第一种:for循环 循环结构for语句的格式:       for(初始化表达式;条件表达式;循环后的操作表达式 ...

  7. MVC异步 导入excel文件

    View页面 js文件.封装到一个js文件里面 (function ($) { //可以忽略 var defaultSettings = { url: "http://upload.zhtx ...

  8. Demo02_对结构体进行文件读写_张仕传_作业_

    #include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...

  9. 九度OJ 1209 最小邮票数 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3 ...

  10. sql server 2008数据复制方法

    发布订阅份为两个步骤:1.发布.2.订阅.首先在数据源数据库服务器上对需要同步的数据进行发布,然后在目标数据库服务器上对上述发布进行订阅.发布可以发布一张表的部分数据,也可以对整张表进行发布.下面分别 ...