开发者

how to use custom designed numbers in a widget

I found a short tutorial describing how to use png's as custom designed numbers. However, I can't get it to work properly. The method intToImage() translates the number to the image, but I can't get it to show the clock using the images.

This is my code:

    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) ? FORMA开发者_如何转开发T_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();
        }
    };
 }
 }

It throws this error: context cannot be resolved.

I know that's because it's not been declared in update(), but how would I accomplish this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜