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

所属分类: 软件编程 / Android 阅读数: 27
收藏 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中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多