简体   繁体   中英

Android widget change update time

I'm using this code to schedule my widget

// Timer
Timer timer = new Timer();
// Schedule time
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);

private class MyTime extends TimerTask {
    // RemoveViews
    RemoteViews remoteViews;
    // AppWidgetManager
    AppWidgetManager appWidgetManager;
    // ComponentName
    ComponentName thisWidget;

    public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(),
        R.layout.main);
    thisWidget = new ComponentName(context, WMTWidget.class);
    }

    @Override
    public void run() {
        //code to update textview
    }
}

This updates a textview every minute which works fine

I'm wondering if it's possible to change this after the first run ? it should update the textview the first time after 1 minute and then every 15 minutes

Is this possible?

Try This It may work

// Timer

int time=1; //Defined it  global

Timer timer = new Timer();
// Schedule time

timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), time, 60000);

     time=15; //update time for 15 minutes Now it will take 15 minute interval

private class MyTime extends TimerTask {
    // RemoveViews
    RemoteViews remoteViews;
    // AppWidgetManager
    AppWidgetManager appWidgetManager;
    // ComponentName
    ComponentName thisWidget;

    public MyTime(Context context, AppWidgetManager appWidgetManager) {

        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(),
        R.layout.main);
    thisWidget = new ComponentName(context, WMTWidget.class);
    }

    @Override
    public void run() {
        //code to update textview
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM