android数字时钟小部件消失了

时间:2013-10-31 12:46:08

标签: android clock android-appwidget digital

我正在为Android开发一个数字时钟小部件,这里是我的代码,但我的问题是我的小部件已经消失了我的意思是没有布局所以任何帮助? 首先启用:

public void onEnabled(Context context) {
    super.onEnabled(context);
    PendingIntent anIntent=PendingIntent.getBroadcast(context, 0, new Intent("upp"),PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmMgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmMgr.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),1000, anIntent);
} 

然后接收覆盖:

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("upp")){
        ComponentName componentName =new ComponentName(context,getClass().getName());
        AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
        int[] appWidgetIds=appWidgetManager.getAppWidgetIds(componentName);
        onUpdate(context,appWidgetManager,appWidgetIds);
    }
}

并在更新时:

Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    super.onUpdate(context, appWidgetManager, appWidgetIds);

然后在onUpdate内部我首先使用Time方法将小时和分钟作为字符串:

    onEnabled(context);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();

int hour = today.hour;
String hh = Integer.toString(hour);

int minute = today.minute;
String mm = ":" + Integer.toString(minute);

然后我用位图在两个图像视图中显示了这两个字符串:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

views.setImageViewBitmap(R.id.imageView1, getFontBitmap(context, hh, Color.WHITE, 70));

views.setImageViewBitmap(R.id.imageView2, getFontBitmap1(context, mm, Color.WHITE, 70));

ComponentName componentName =new ComponentName(context,getClass().getName());
int[] appWidgetId=appWidgetManager.getAppWidgetIds(componentName);
appWidgetManager.updateAppWidget(appWidgetId, views);
so any help? Thanks in advance

0 个答案:

没有答案