1

(请!!任何人都可以帮助我)' :) 嗨

我正在开发一个数字时钟小部件:

先生是我的代码:

首先在启用和接收覆盖上:

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);
} 

我的问题是当我将此小部件添加到主屏幕时没有布局我的意思是我的时钟不见了但是当我删除接收覆盖时我可以看到我的小部件但它没有更新?提前致谢

4

0 回答 0