我们看到很多手机的待机界面,都有一个漂亮的数字时钟,而我们的AppWidget上面却没有数字时钟的待机控件。今天我们就动手开发一个数字时钟控件。
首先,我们准备十一张图片,分别是背景图标,冒号图片和数字0到9.
准备AppWidget_Info.xml,AppWidget_Layout.xml,AppWidget_Provider.java.
下面贴上主要代码:
AppWidget_Info.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/widget_layout"
>
</appwidget-provider>
AppWidget_Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/bg_02">
<ImageView
android:id="@+id/clock1"
android:layout_marginLeft="12dip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/seledatebg"/>
<ImageView
android:id="@+id/clock3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
</LinearLayout>
AppWidget_Provider.java
/**
* AppWidget的实例第一次被创建时调用
* */
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//获取系统时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(calendar.HOUR_OF_DAY);
int minute = calendar.get(calendar.MINUTE);
//remoteViews的初始化
remoteViews.setImageViewResource(R.id.clock1, R.drawable.n0+hour/10);
remoteViews.setImageViewResource(R.id.clock2, R.drawable.n0+hour%10);
remoteViews.setImageViewResource(R.id.clock3, R.drawable.n0+minute/10);
remoteViews.setImageViewResource(R.id.clock4, R.drawable.n0+minute%10);
super.onEnabled(context);
}
/**
* 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
* */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);
//创建一个Intent对象
Intent intent = new Intent();
intent.setAction(BroadcastString);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//remoteViews.setImageViewResource(R.id.root, R.drawable.bg_01);
//remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
//获取系统时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(calendar.HOUR_OF_DAY);
int minute = calendar.get(calendar.MINUTE);
//remoteViews的初始化
remoteViews.setImageViewResource(R.id.clock1, R.drawable.n0+hour/10);
remoteViews.setImageViewResource(R.id.clock2, R.drawable.n0+hour%10);
remoteViews.setImageViewResource(R.id.clock3, R.drawable.n0+minute/10);
remoteViews.setImageViewResource(R.id.clock4, R.drawable.n0+minute%10);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
//super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/**
* 接受广播事件
* */
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(BroadcastString)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, AppWidgetTestProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
super.onReceive(context, intent);
}
首先,我们准备十一张图片,分别是背景图标,冒号图片和数字0到9.
准备AppWidget_Info.xml,AppWidget_Layout.xml,AppWidget_Provider.java.
下面贴上主要代码:
AppWidget_Info.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/widget_layout"
>
</appwidget-provider>
AppWidget_Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/bg_02">
<ImageView
android:id="@+id/clock1"
android:layout_marginLeft="12dip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/seledatebg"/>
<ImageView
android:id="@+id/clock3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
<ImageView
android:id="@+id/clock4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://chaod5659.blog.163.com/blog/@drawable/n0"
/>
</LinearLayout>
AppWidget_Provider.java
/**
* AppWidget的实例第一次被创建时调用
* */
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//获取系统时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(calendar.HOUR_OF_DAY);
int minute = calendar.get(calendar.MINUTE);
//remoteViews的初始化
remoteViews.setImageViewResource(R.id.clock1, R.drawable.n0+hour/10);
remoteViews.setImageViewResource(R.id.clock2, R.drawable.n0+hour%10);
remoteViews.setImageViewResource(R.id.clock3, R.drawable.n0+minute/10);
remoteViews.setImageViewResource(R.id.clock4, R.drawable.n0+minute%10);
super.onEnabled(context);
}
/**
* 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
* */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);
//创建一个Intent对象
Intent intent = new Intent();
intent.setAction(BroadcastString);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//remoteViews.setImageViewResource(R.id.root, R.drawable.bg_01);
//remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
//获取系统时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(calendar.HOUR_OF_DAY);
int minute = calendar.get(calendar.MINUTE);
//remoteViews的初始化
remoteViews.setImageViewResource(R.id.clock1, R.drawable.n0+hour/10);
remoteViews.setImageViewResource(R.id.clock2, R.drawable.n0+hour%10);
remoteViews.setImageViewResource(R.id.clock3, R.drawable.n0+minute/10);
remoteViews.setImageViewResource(R.id.clock4, R.drawable.n0+minute%10);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
//super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/**
* 接受广播事件
* */
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(BroadcastString)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, AppWidgetTestProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
super.onReceive(context, intent);
}