Android RemoteView的应用 三 新年倒计时通知栏
2017-04-10 16:55
459 查看
这个新年倒计时桌面小部件制作很简单,只是通过Timer不断刷新RemoteView就能做到
1. 在res/layout新建main作为部件的界面
在res/xml新建widget_provider配置部件界面的大小,刷新时间
3.部件实现类
4 . 注册文件
再见
1. 在res/layout新建main作为部件的界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ddd"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/wordcup" android:text="hello new year" android:textSize="20dp" android:textColor="#100f0f"/> </LinearLayout>
在res/xml新建widget_provider配置部件界面的大小,刷新时间
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/main" android:minHeight="40dp" android:minWidth="80dp" android:updatePeriodMillis="10000"> </appwidget-provider>
3.部件实现类
public class WidetDemo extends AppWidgetProvider { @Override public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){ super.onUpdate(context, appWidgetManager, appWidgetIds); Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000); } private class MyTime extends TimerTask{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; public MyTime(Context context,AppWidgetManager appWidgetManager){ this.appWidgetManager = appWidgetManager; remoteViews = new RemoteViews(context.getPackageName(),R.layout.main); thisWidget = new ComponentName(context,WidetDemo.class); } @Override public void run() { Date date = new Date(); Calendar calendar = new GregorianCalendar(2018,01,01); long days = (((calendar.getTimeInMillis()-date.getTime())/1000)/86400); remoteViews.setTextViewText(R.id.wordcup,"距离新年还有"+days+"天"); appWidgetManager.updateAppWidget(thisWidget,remoteViews); } } }
4 . 注册文件
<receiver android:name=".WidetDemo"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider"/> </receiver>
再见
相关文章推荐
- 最最基础的Android倒计时应用
- Android开发之倒计时应用(二)
- 【Android应用开发】Timer的使用,制作游戏倒计时进度条
- Android开发之倒计时应用(三)
- Android倒计时项目中的应用
- Android应用界面开发——Handler(实现倒计时)
- Android开发之倒计时应用(一)
- Android自定义view之(CSDN应用splash界面的倒计时View)
- Android RemoteView的应用 一 桌面控件
- Android RemoteView的应用 二 通知栏
- (android应用)考试倒计时一------------------功能和界面设计
- Android应用中ListView实现多条数据同时倒计时功能
- Android四大组件应用系列——使用BroadcastReceiver和Service实现倒计时
- Android进阶——Android常见项目模块之倒计时、定时和延时的应用
- Android中构建数据业务应用
- 谷歌Android手机应用开发环境的搭建 推荐
- Android是一个应用框架,而不是类别库
- 《Android 应用开发课程大纲》 V1.0
- 纯JS的基于静态站点服务器时间 [无依赖] 的倒计时程序(Ajax应用)
- Google手机操作系统Android应用开发入门