当前位置: 首页 > news >正文

新建南昌网站建设公司/长春seo按天计费

新建南昌网站建设公司,长春seo按天计费,用vs2015做网站,wordpress 添加新页面桌面Widget其实就是一个显示一些信息的工具(现在也有人开发了一些有实际操作功能的widget。例如相机widget,可以直接桌面拍照)。不过总的来说,widget主要功能就是显示一些信息。我们今天编写一个很简单的作为widget,显…

桌面Widget其实就是一个显示一些信息的工具(现在也有人开发了一些有实际操作功能的widget。例如相机widget,可以直接桌面拍照)。不过总的来说,widget主要功能就是显示一些信息。我们今天编写一个很简单的作为widget,显示时间、日期、星期几等信息。需要显示时间信息,那就需要实时更新,一秒或者一分钟更新一次。

这个时间Widget我是参考(Android应用开发揭秘)书里面的一个demo例子做的,只是把功能和界面完善了一下。下面是这次的效果图:


 

1、继承AppWidgetProvider

我们编写的桌面Widget需要提供数据更新,这里就需用用到AppWidgetProvider,它里面有一些系统回调函数。提供更新数据的操作。AppWidgetProvider是BrocastReceiver的之类,也就是说它其实本质是一个广播接收器。下面我们看看AppWidgetProvider的几个重要的回调方法:

 

public class WidgetProvider extends AppWidgetProvider 
{private static final String TAG="mythou_Widget_Tag";// 没接收一次广播消息就调用一次,使用频繁public void onReceive(Context context, Intent intent) {Log.d(TAG, "mythou--------->onReceive");super.onReceive(context, intent);}// 每次更新都调用一次该方法,使用频繁public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {Log.d(TAG, "mythou--------->onUpdate");super.onUpdate(context, appWidgetManager, appWidgetIds);}// 没删除一个就调用一次public void onDeleted(Context context, int[] appWidgetIds) {Log.d(TAG, "mythou--------->onDeleted");super.onDeleted(context, appWidgetIds);}// 当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用public void onEnabled(Context context) {Log.d(TAG, "mythou--------->onEnabled");super.onEnabled(context);}// 当最后一个该Widget删除是调用该方法,注意是最后一个public void onDisabled(Context context) {Log.d(TAG, "mythou--------->onDisabled");super.onDisabled(context);}
}

 

 

其中我们比较常用的是onUpdate和onDelete方法。我这里刷新时间使用了一个Service,因为要定时刷新服务,还需要一个Alarm定时器服务。下面给出我的onUpdate方法:

 

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{super.onUpdate(context, appWidgetManager, appWidgetIds);Time time = new Time();time.setToNow();//使用Service更新时间Intent intent = new Intent(context, UpdateService.class);PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);//使用Alarm定时更新界面数据AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);alarm.setRepeating(AlarmManager.RTC, time.toMillis(true), 60*1000, pendingIntent);
}


 

2、AndroidManifest.xml配置

 

  <application android:icon="@drawable/icon" android:label="@string/app_name"><!-- AppWidgetProvider的注册 mythou--><receiver android:label="@string/app_name_timewidget" android:name="com.owl.mythou.TimeWidget"><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action></intent-filter><meta-data android:name="android.appwidget.provider" android:resource="@xml/time_widget_config"></meta-data></receiver><!-- 更新时间的后台服务 mythou--><service android:name="com.owl.mythou.UpdateService"></service></application>


AndroidManifest主要是配置一个receiver,因为AppWidgetProvider就是一个广播接收器。另外需要注意的是,里面需要提供一个action,这个是系统的更新widget的action。还有meta-data里面需要指定widget的配置文件。这个配置文件,需要放到res\xml目录下面,下面我们看看time_widget_config.xml的配置

 

 

3、appWidget配置:

复制代码
//Edited by mythou
//http://www.cnblogs.com/mythou/
<?xml version="1.0" encoding="utf-8"?>
<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"android:initialLayout="@layout/time_widget_layout"android:minWidth="286dip" android:minHeight="142dip" android:updatePeriodMillis="0">
</appwidget-provider>
复制代码
  • android:initialLayout 指定界面布局的Layout文件,和activity的Layout一样
  • android:minWidth 你的widget的最小宽度。根据Layout的单元格计算(72*格子数-2)
  • android:minHeigh 你的widget的最小高度。计算方式和minwidth一样。(对这个不了解可以看我Launcher分析文章)
  • android:updatePerioMillis 使用系统定时更新服务,单位毫秒。

这里需要说明android:updatePerioMillis的问题,系统为了省电,默认是30分钟更新一次,如果你设置的值比30分钟小,系统也是30分钟才会更新一次。对于我们做时间Widget来说,显然不靠谱。所以只能自己编写一个Alarm定时服务更新。

 

4、更新Widget的Service服务

 

public class UpdateService extends Service
{@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);UpdateWidget(this);}private void UpdateWidget(Context context) {    //不用Calendar,Time对cpu负荷较小Time time = new Time();time.setToNow();int hour = time.hour;int min = time.minute;int second = time.second;int year = time.year;int month = time.month+1;int day = time.monthDay;String strTime = String.format("%02d:%02d:%02d %04d-%02d-%02d", hour, min, second,year,month,day);RemoteViews updateView = new RemoteViews(context.getPackageName(),R.layout.time_widget_layout);//时间图像更新String packageString="org.owl.mythou";String timePic="time";int hourHbit = hour/10;updateView.setImageViewResource(R.id.hourHPic, getResources().getIdentifier(timePic+hourHbit, "drawable", packageString));int hourLbit = hour%10;updateView.setImageViewResource(R.id.hourLPic, getResources().getIdentifier(timePic+hourLbit, "drawable", packageString));int minHbit = min/10;updateView.setImageViewResource(R.id.MinuteHPic, getResources().getIdentifier(timePic+minHbit, "drawable", packageString));int minLbit = min%10;updateView.setImageViewResource(R.id.MinuteLPic, getResources().getIdentifier(timePic+minLbit, "drawable", packageString));//星期几updateView.setTextViewText(R.id.weekInfo, getWeekString(time.weekDay+1));//日期更新,根据日期,计算使用的图片String datePic="date";int year1bit = year/1000;updateView.setImageViewResource(R.id.Year1BitPic, getResources().getIdentifier(datePic+year1bit, "drawable", packageString));int year2bit = (year%1000)/100;updateView.setImageViewResource(R.id.Year2BitPic, getResources().getIdentifier(datePic+year2bit, "drawable", packageString));int year3bit = (year%100)/10;updateView.setImageViewResource(R.id.Year3BitPic, getResources().getIdentifier(datePic+year3bit, "drawable", packageString));int year4bit = year%10;updateView.setImageViewResource(R.id.Year4BitPic, getResources().getIdentifier(datePic+year4bit, "drawable", packageString));//月int mouth1bit = month/10;updateView.setImageViewResource(R.id.mouth1BitPic, getResources().getIdentifier(datePic+mouth1bit, "drawable", packageString));int mouth2bit = month%10;updateView.setImageViewResource(R.id.mouth2BitPic, getResources().getIdentifier(datePic+mouth2bit, "drawable", packageString));//日int day1bit = day/10;updateView.setImageViewResource(R.id.day1BitPic, getResources().getIdentifier(datePic+day1bit, "drawable", packageString));int day2bit = day%10;updateView.setImageViewResource(R.id.day2BitPic, getResources().getIdentifier(datePic+day2bit, "drawable", packageString));//点击widget,启动日历Intent launchIntent = new Intent();launchIntent.setComponent(new ComponentName("com.mythou.mycalendar","com.mythou.mycalendar.calendarMainActivity"));launchIntent.setAction(Intent.ACTION_MAIN);launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);PendingIntent intentAction = PendingIntent.getActivity(context, 0,launchIntent, 0);updateView.setOnClickPendingIntent(R.id.SmallBase, intentAction);AppWidgetManager awg = AppWidgetManager.getInstance(context);awg.updateAppWidget(new ComponentName(context, TimeWidgetSmall.class),updateView);}
}


 

上面就是我的Service,因为我的界面时间和日期都是使用图片做的(纯属为了好看点)。所以多了很多根据时间日期计算使用的图片名字的代码,这些就是个人实际处理,这里不多说。

有一点需要说明的是RemoteViews

 

RemoteViews updateView = new RemoteViews(context.getPackageName(), R.layout.time_widget_layout);

 

从我们的界面配置文件生成一个远程Views更新的对象,这个可以在不同进程中操作别的进程的View。因为Widget是运行在Launcher的进程里面的,而不是一个独立的进程。这也是一种远程访问机制。最后就是加了一个点击桌面Widget启动一个程序的功能,也是使用了PendingIntent的方法。

编写一个桌面Widget主要就是这些步骤,最后补充一点,桌面Widget的界面布局只支持一部分android的标准控件,如果需要做复杂widget界面,需要自定义控件。这部分后面有时间再说~

 

转载于:https://www.cnblogs.com/pangblog/p/3239138.html

http://www.jmfq.cn/news/4899925.html

相关文章:

  • 电子商务平台网站源码/口碑营销成功案例有哪些
  • 微信朋友圈做网站推广赚钱吗/关键词优化排名软件哪家好
  • 建设部颁发的证书网站/如何进行品牌宣传与推广
  • 怎么做网站适配/怎样在平台上发布信息推广
  • 临沂网站设计/做关键词优化的公司
  • 网站建设和网络优化/seo从零开始到精通200讲解
  • 重庆大学建设管理与房地产学院网站/一手app推广接单平台
  • 使用iis6搭建网站/北京朝阳区优化
  • wordpress仿p站主题/百家港 seo服务
  • 哈尔滨信息网免费招聘/长沙seo霜天
  • 廊坊网站搜索优化/2345王牌浏览器
  • 东莞优化网站建设/广州营销seo
  • 做任务挣钱的网站聚/页面优化的方法有哪些
  • 珠海响应式网站建设推广公司/抖音关键词排名系统
  • 建一个电商网站多少钱/宁波seo外包推广公司
  • 聊城网站公司/软件培训班学费多少
  • 厦门网络建站公司/百度网页提交入口
  • 网站模板 安全吗/网站制作定制
  • 手机怎样设计网站建设/网站排行
  • 十堰seo优化方法/惠州seo快速排名
  • 专业网站建设公司兴田德润放心/哈尔滨seo
  • 外贸网站 在线留言/白帽seo是什么
  • 网站建设方案 doc/搜索引擎优化介绍
  • 怎样免费做公司网站/百度知道
  • 网络舆情系统/搜索引擎优化概述
  • 网站建设管理工作经验介绍/开网站需要什么流程
  • 网站开发文档总结/seo经理
  • 搜房网站要怎么 做/百度账号一键登录
  • 温州网站建设培训班/市场营销推广策划方案
  • 长沙有哪些招聘网站/网络推广销售是做什么的