android之App Widget开发实例代码解析

所属分类: 软件编程 / Android 阅读数: 53
收藏 0 赞 0 分享

Android Widget开发案例实现是本文要介绍的内容,主要是来了解并学习Android Widget开发应用,今天我们要写一下Android Widget的开发,由于快点凌晨,我就不说的太具体了,同志们就模仿吧!

首先继续了解下App Widget框架的主要的类:

AppWidgetProvider:继承自BroadcastReceiver,在App Widget应用update,enable,disable和deleted时接受通知。其中onUpdate,onReceive是最常用到的方法。

AppWidgetProviderInfo:描述AppWidget的大小,更新频率和初始界面等信息,以xml文件的形式存在于应用中的res/xml目录下。

 AppWidgetManager:负责管理AppWidget,向AppWidgetProvider发送通知。

RemoteViews:一个可以在其他应用进程中运行的类,是构造AppWidget的核心。

下面开始代码的编写,首先在res/xml下建立myappwidetprovider.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
  android:minWidth="100dp" 
  android:minHeight="72dp" 
  android:updatePeriodMillis="86400000" 
  android:initialLayout="@layout/myappwidget" 
  > 
</appwidget-provider> 

上面分别是 定义widget的宽度,高度,更新周期,以及layout的widget布局。

下面是我们的布局文件:
 

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="@drawable/widget_bg1" 
  android:gravity="center" 
  android:id="@+id/layout" 
  android:orientation="vertical" > 
 
  <TextView 
    android:id="@+id/txtMonth" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:layout_margin="2dp" 
    android:text="" /> 
<TextView 
    android:id="@+id/txtDay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#990033" 
    android:textSize="25dp" 
    android:text="" /> 
<TextView 
    android:id="@+id/txtWeekDay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:textColor="#000000" 
    android:text="" /> 
</LinearLayout> 

对应布局widget要求比较高,大家自行设计,更加美观的界面。

接下来是我们的核心代码ExampleAppWidgetProvider类了:
 

 import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.text.format.Time; 
import android.widget.RemoteViews; 
import android.widget.Toast; 
 
public class ExampleAppWidgetProvider extends AppWidgetProvider{ 
  private String[] months={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; 
  private String[] days={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; 
  @Override 
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
    // TODO Auto-generated method stub 
     
    RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.myappwidget); 
    Time time=new Time(); 
    time.setToNow(); 
    String month=time.year+" "+months[time.month]; 
    remoteViews.setTextViewText(R.id.txtDay, new Integer(time.monthDay).toString()); 
    remoteViews.setTextViewText(R.id.txtMonth, month); 
    remoteViews.setTextViewText(R.id.txtWeekDay, days[time.weekDay]); 
    Intent intent=new Intent("cn.com.karl.widget.click"); 
    PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0); 
    remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent); 
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
     
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
  } 
   
  @Override 
  public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    super.onReceive(context, intent); 
    if(intent.getAction().equals("cn.com.karl.widget.click")){ 
      Toast.makeText(context, "点击了widget日历", 1).show(); 
    } 
  } 
} 

上面代码忘记做注释了,在这类分别解释下,使用remoteViews类分别加载上来布局文件的相应ID设置好值,然PendingIntent 这就没什么好解释的了。

最后在manifest中加入:

 <receiver android:name="ExampleAppWidgetProvider" > 
      <intent-filter > 
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
        <action android:name="cn.com.karl.widget.click" > 
        </action> 
      </intent-filter> 
 
      <meta-data 
        android:name="android.appwidget.provider" 
        android:resource="@xml/myappwidetprovider" /> 
    </receiver> 

 这样就完成了,运行项目看一下载手机上运行的效果吧:

上面就是我们自己定义的AppWidget显示效果,点击它:
这里为了表示点击了它,使用了Toast打印信息,当然我们也可以点击它之后启动相应的Activity。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多