Java 不使用Runnable和Timer类定期调用方法
我想知道是否可以在不使用Runnable和Timer类的情况下定期调用方法。简单地说,如何在不创建新线程的情况下调用方法? 多谢各位 我想知道是否可以在不使用Runnable和Timer类的情况下定期调用方法 虽然您可以避免使用Java 不使用Runnable和Timer类定期调用方法,java,android,timer,runnable,Java,Android,Timer,Runnable,我想知道是否可以在不使用Runnable和Timer类的情况下定期调用方法。简单地说,如何在不创建新线程的情况下调用方法? 多谢各位 我想知道是否可以在不使用Runnable和Timer类的情况下定期调用方法 虽然您可以避免使用计时器,但您需要在这个周期性的基础上触发一些东西 例如,您可以让一个运行正常的Runnable,然后在任何视图(或处理程序)上调用postdayed() /*** Copyright (c) 2012 CommonsWare, LLC Licensed under
计时器
,但您需要在这个周期性的基础上触发一些东西
例如,您可以让一个运行正常的Runnable
,然后在任何视图
(或处理程序
)上调用postdayed()
/***
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);
}
}
(来自)
我想知道是否可以在不使用Runnable和Timer类的情况下定期调用方法
虽然您可以避免使用计时器
,但您需要在这个周期性的基础上触发一些东西
例如,您可以让一个运行正常的Runnable
,然后在任何视图
(或处理程序
)上调用postdayed()
/***
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);
}
}
(from)由于提到了AlarmManager,如果您的目标是API级别21及以上,则可以使用作业调度器(较低级别的API应使用AlarmManager):
这是一个API,用于根据
将在应用程序自己的进程中执行的框架
该框架将在您收到您的
回调,并尽可能尝试批处理和延迟回调。
通常,如果您没有指定作业的截止日期,则可以运行该作业
根据作业计划程序的当前状态,随时
内部队列,但它可能会延迟到下一个队列
设备连接到电源的时间
您不直接实例化这个类;取而代之的是取回它
通过Context.getSystemService(Context.JOB\u SCHEDULER\u SERVICE)
JobService
的schedule
方法需要一个可以配置周期性延迟的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());
由于已提到AlarmManager,如果您的目标是API级别21及以上,则可以使用作业调度器(对于较低级别的API,应使用AlarmManager):
这是一个API,用于根据
将在应用程序自己的进程中执行的框架
该框架将在您收到您的
回调,并尽可能尝试批处理和延迟回调。
通常,如果您没有指定作业的截止日期,则可以运行该作业
根据作业计划程序的当前状态,随时
内部队列,但它可能会延迟到下一个队列
设备连接到电源的时间
您不直接实例化这个类;取而代之的是取回它
通过Context.getSystemService(Context.JOB\u SCHEDULER\u SERVICE)
JobService
的schedule
方法需要一个可以配置周期性延迟的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());
是的,这叫alarmmanager。你为什么想要这样的东西?再想想。定期打电话会涉及到很多重复的等待。在此期间,您的主线程无法执行任何其他操作,包括按键服务等。这就是为什么网络活动必须在单独的线程上执行,以避免锁定主应用程序线程。使用处理程序,它不会创建任何线程。是的,它被称为alarmmanager。您为什么想要这样的事情?请仔细想想。定期打电话会涉及到很多重复的等待。在此期间,你的主线程无法执行任何其他操作,包括按键服务等。这就是为什么网络活动必须在单独的线程上执行,以避免锁定主应用程序线程。使用处理程序,它不会创建任何线程