我正在为 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