ShowTimeNowAppWidget
package com.zhy.shortcut;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
public class ShowTimeNowAppWidget extends AppWidgetProvider {
private static final String TAG = "ShowTimeNowAppWidget";
Handler handler;
AppWidgetManager widgetManager;
RemoteViews remoteViews;
Context context;
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Log.d(TAG, "---------onDeleted----------");
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d(TAG, "---------onDisabled----------");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d(TAG, "---------onEnabled----------");
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d(TAG, "---------onReceive----------");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
this.context=context;
final int appWidgetId=appWidgetIds[0];
//创建一个RemoteViews
RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widgetlayout);
widgetManager=appWidgetManager;
remoteViews=views;
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
//ComponentName component=new ComponentName(ShowTimeNowAppWidget.this.context, ShowTimeNowAppWidget.class);
//更新widget上TextView控件的内容
remoteViews.setTextViewText(R.id.widget_time, String.valueOf(msg.obj));
//更新widget
widgetManager.updateAppWidget(appWidgetId, remoteViews);
break;
default:
break;
}
}
};
handler.post(new Runnable() {
@Override
public void run() {
Message message=new Message();
message.what=0;
message.obj=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
handler.sendMessage(message);
handler.postDelayed(this, 1000);
}
});
Log.d(TAG, "---------onUpdate----------");
}
}
widgetlayout布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="30dip"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/widget_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dialog_time" />
<TextView
android:textSize="16dip"
android:textColor="@color/green"
android:singleLine="true"
android:ellipsize="none"
android:gravity="center"
android:paddingLeft="5dip"
android:layout_toRightOf="@id/widget_img"
android:layout_alignTop="@id/widget_img"
android:layout_alignBottom="@id/widget_img"
android:id="@+id/widget_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</RelativeLayout>
appwidget-provider描述文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="300dip"
android:minHeight="20dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/widgetlayout"
>
</appwidget-provider>
AndroidManifest.xml中的配置
<receiver
android:name=".ShowTimeNowAppWidget"
>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>
运行效果