如何在窗口小部件中使用自定义设计的数字

时间:2010-12-03 16:36:48

标签: android numbers widget

我找到了一个简短的教程,描述了如何将png用作自定义设计的数字。但是,我无法让它正常工作。方法 intToImage()将数字转换为图像,但我无法使用图像显示时钟。

这是我的代码:

    public static final class UpdateService extends Service {

   static final String ACTION_UPDATE = "android.tristan.widget.digiclock.action.UPDATE";

    private final static IntentFilter sIntentFilter;

    private final static String FORMAT_12_HOURS = "h:mm";
    private final static String FORMAT_24_HOURS = "kk:mm";

    private String mTimeFormat;
    private String mDateFormat;
    private String mDayFormat;
    private Calendar mCalendar;

    static {
        sIntentFilter = new IntentFilter();
        sIntentFilter.addAction(Intent.ACTION_TIME_TICK);
        sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        reinit();
        registerReceiver(mTimeChangedReceiver, sIntentFilter);
     }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mTimeChangedReceiver);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        if (ACTION_UPDATE.equals(intent.getAction())) {
            update();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private void update() {
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        final CharSequence time = DateFormat.format(mTimeFormat, mCalendar);
        final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
        final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);      
        views.setTextViewText(R.id.Time, time);
        views.setTextViewText(R.id.Day, day);
        views.setTextViewText(R.id.Date, date);
        **RemoteViews Timeview = new RemoteViews(getPackageName(), R.id.Timeview);
        intToImage(context, time, 0, Timeview);**
        ComponentName widget = new ComponentName(this, DigiClock.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(widget, views);
    }
    **
        private void intToImage(Context context, int numberInt, int where, RemoteViews remoteView) {

         if (numberInt < 0 || numberInt > 9) {
            Log.d("Tutorial", "Invalid number" + numberInt);
    }   
        else {
            int whereArray[] = { R.id.Timeview }; // viewId
            int images[] = { R.drawable.zero, R.drawable.one, R.drawable.two,
                            R.drawable.three, R.drawable.four, R.drawable.five,
                            R.drawable.six, R.drawable.seven, R.drawable.eight,
                            R.drawable.nine }; // srcId
            remoteView.setImageViewResource(whereArray[where], images[numberInt]);
    }

}**

    private void reinit() {
        mDayFormat = getString(R.string.day_format);
        mDateFormat = getString(R.string.date_format);
        mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS;
        mCalendar = Calendar.getInstance();
    }

    private static boolean is24HourMode(final Context context) {
        return android.text.format.DateFormat.is24HourFormat(context);
    }

    private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                action.equals(Intent.ACTION_TIMEZONE_CHANGED))
            {
                reinit();
            }
            update();
        }
    };
 }
 }

它会抛出错误上下文无法解析

我知道那是因为它没有在 update()中声明,但我将如何做到这一点?

0 个答案:

没有答案