android从widget调用一个timer run函数

时间:2012-02-08 16:23:37

标签: java android

我的小部件中有一个计时器,每10分钟运行一次,但是当我按下按钮时我需要调用计时器运行功能。有没有办法做到这一点?

public class MyWidget extends AppWidgetProvider {


@Override
public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
  // check, if our Action was called
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            //call the run function of the timer
        }

        super.onReceive(context, intent);
    }
}

@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) {

    if(tt==0) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1000, 600000);
        tt=1;
    }
}

private class MyTime extends TimerTask {


    public MyTime(Context context, AppWidgetManager appWidgetManager) {

    }

    @Override
    public void run() {
            }

我需要从onReceive()

中调用该函数

2 个答案:

答案 0 :(得分:0)

您可以在MyWidget类中创建一个方法,并从MyTime和onReceive中的run方法调用它,如下所示:

public class MyWidget extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
            }
        } else {
            // check, if our Action was called
            if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                //call the run function of the timer
                doStuff();
            }
            super.onReceive(context, intent);
        }
    }
    @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) {
        if(tt==0) {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1000, 600000);
            tt=1;
        }
    }

    private class MyTime extends TimerTask {
        public MyTime(Context context, AppWidgetManager appWidgetManager) {

        }

        @Override
        public void run() {
            doStuff();
        }
    }

    private void doStuff() {
        // Stuff that needs to be done in run and onReceive
    }
}

答案 1 :(得分:0)

您指的是哪种运行方法?

类Timer()

中没有run方法

如果您尝试在MyTime类中执行run方法,请通过线程启动它,因为TimerTask实现了Runnable -

  if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
        //call the run function of the timer
        MyTime myTime = new MyTime();
        Thread thread = new Thread(myTime);
        thread.start();
    }