详解Android Widget笔记学习教程

移动开发
Android Widget笔记学习教程是本文要介绍的内容,主要是来了解并学习Android Widget中的应用,具体内容的实现来看本文详解。

Android Widget笔记学习教程是本文要介绍的内容,主要是来了解并学习Android Widget中的应用,具体内容的实现来看本文详解。学了那么久居然今天才晓得widget.我太惭愧了。

首先记录下基本的步骤吧

(1)总的来说就是修改三个XML,一个class...

(2)***个xml是布局XML文件(如:main.xml),是这个widget的。一般来说如果用这个部件显示时间,那就只在这个布局XML中声明一个textview就OK了

(3)第二个xml是widget_provider.xml,主要是用于声明一个appwidget的 (其中:updatePeriodMillis是定时更新时间、每秒都会调用该 appwidget的onUpdate方法的作用、Layout就是指定上面那个main.xml)

(4)第三个是AndroidManifest.xml中,注册broadcastReceiver信息

(5)***那个class用于做一些业务逻辑操作

具体的一些代码

1、main.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/tvCurrTime" 
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:text="@string/hello" 
    android:textColor="@color/black" 
    /> 
</LinearLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

2、hello_widget_provider.xml部分

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

<!-- appwidget的updatePeriodMillis:定时更新时间、意思是每秒都会调用该 appwidget的onUpdate方法,onUpdate方法在两种情况下被调用,***种是添加appwidget时,第二种是每一个更新周期结束时调用一次onUpdate方法。注:此属性在SDK1.5版本以后就失效了!!!如果需要更新的话必须自己写个定时器哈,我的版本为2.2) -->

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
android:minWidth="146dip" 
android:minHeight="72dip" 
android:initialLayout="@layout/main"> 
</appwidget-provider> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

3、AndroidManifest.xml部分

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.woody.testWidget" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
      <!-- HelloWidgetProvider为那个class(业务处理) --> 
 
<receiver android:name=".HelloWidgetProvider" android:label="@string/app_name"> 
    <intent-filter> 
    <!-- 指定了的 --> 
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
    <meta-data android:name="android.appwidget.provider" 
 
<!-- 为上面指定了的widget --> 
    android:resource="@xml/hello_widget_provider" /> 
   </receiver> 
    </application> 
</manifest> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

4、HelloWidgetProvider类的部分(进行时间的计算,核心onUpdate方法部分)

@Override  
public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
    int[] appWidgetIds) {  
   Timer timer = new Timer();  
   timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

5、MyTime(自己写的定时器)

public class MyTime extends TimerTask {  
 
RemoteViews remoteViews;  
AppWidgetManager appWidgetManager;  
ComponentName thisWidget;  
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,  
Locale.getDefault());  
public MyTime(Context context, AppWidgetManager appWidgetManager) {  
this.appWidgetManager = appWidgetManager;  
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);  
thisWidget = new ComponentName(context, HelloWidgetProvider.class);  
}  
@Override  
public void run() {  
remoteViews.setTextViewText(R.id.tvCurrTime,  
"Time = " + format.format(new Date()));  
appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

小结:详解Android Widget笔记学习教程的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-09-07 16:28:46

QT WidgetQWidget

2011-09-07 11:15:25

2011-09-07 13:00:36

2011-09-07 13:42:36

Android Wid实例

2011-09-09 11:05:56

Widget

2010-07-13 09:02:19

Widget开发

2011-09-09 16:38:51

Android Wid源码

2011-09-09 20:14:58

Android Wid

2011-09-07 13:18:40

Android Wid

2011-09-07 17:19:16

Web widget

2011-09-07 16:36:00

Qt Widget

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-08 13:11:07

Android Wid实例

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-09-08 15:40:45

Android Wid组件

2011-09-07 14:25:53

Android Wid设计

2013-06-28 13:38:45

AndroidAndroidMani

2010-06-03 11:12:55

Hadoop

2011-09-07 13:06:04

Android Wid

2011-09-07 14:20:42

Android Wid组件
点赞
收藏

51CTO技术栈公众号