【问题标题】:Call a method periodically without using Runnable and Timer class不使用 Runnable 和 Timer 类定期调用方法
【发布时间】:2013-12-05 15:21:51
【问题描述】:

我想知道是否可以在不使用 Runnable 和 Timer 类的情况下定期调用方法。简而言之,如何在不创建新线程的情况下调用方法? 谢谢。

【问题讨论】:

  • 是的,它叫做alarmmanager。
  • 你为什么要这样的东西?
  • 再想一想。定期调用某些东西将涉及大量重复等待。在此期间,您的主线程不能做任何其他事情,包括服务按键等。这就是为什么必须在单独的线程上完成网络活动以避免锁定主应用线程。
  • 使用 Handler,它不会创建任何线程

标签: java android timer runnable


【解决方案1】:

我想知道是否可以在不使用 Runnable 和 Timer 类的情况下定期调用方法

虽然您可以避免Timer,但您将需要某些东西,该东西将在此定期触发。

例如,您可以有一个可以工作的Runnable,然后在任何View(或Handler)上调用postDelayed(),以安排自己在未来再次运行:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.post;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class PostDelayedDemo extends Activity implements Runnable {
  private static final int PERIOD=5000;
  private View root=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    root=findViewById(android.R.id.content);
  }

  @Override
  public void onResume() {
    super.onResume();

    run();
  }

  @Override
  public void onPause() {
    root.removeCallbacks(this);

    super.onPause();
  }

  @Override
  public void run() {
    Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT)
         .show();
    root.postDelayed(this, PERIOD);
  }
}

(来自this sample project

【讨论】:

    【解决方案2】:

    由于已经提到了 AlarmManager,如果您的目标 API 级别为 21 及以上,您可以使用 JobScheduler(AlarmManager 应该用于较低级别的 API):

    https://developer.android.com/reference/android/app/job/JobScheduler.html#schedule(android.app.job.JobInfo)

    这是一个用于调度各种类型作业的 API 将在您的应用程序自己的进程中执行的框架。

    当您收到您的 回调,并尝试尽可能多地批处理和推迟它们。 通常,如果您没有为您的工作指定截止日期,它可以运行 随时取决于 JobScheduler 的当前状态 内部队列,但是它可能会被推迟到下一个 设备连接到电源的时间。

    你不直接实例化这个类;相反,检索它 通过 Context.getSystemService(Context.JOB_SCHEDULER_SERVICE)。

    JobServiceschedule 方法需要JobInfo,它可以配置周期性延迟。

    一个例子:

            ComponentName serviceComponent = new ComponentName(getContext(), YourService.class);
    
            JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
            builder.setOverrideDeadline(SERVICE_INTERVAL);
            //pass data to your service here.
            builder.setExtras(bundle);
    
            JobScheduler jobScheduler = getContext().getSystemService(JobScheduler.class);
            jobScheduler.schedule(builder.build());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多