当前位置:首页 » 安卓系统 » android定时线程

android定时线程

发布时间: 2022-06-06 00:51:32

㈠ Android 定时执行任务的几种方法

alarmManager
Timer
Handler

㈡ android怎么设置一个定时的任务

我知道的有三种方法;
方法一:可以通过android自带的Handler机制,里面有new Handler().postDelayed(new Runnable(){
@Override
public void run() {
//延迟的时间到了执行
}
},延迟的时间);

方法二:可以通过java里面的线程来写,new Thread().sleep(延迟时间);时间到了会继续执行;
方法三:java定时器(有很多方法)例如:
Timer timer = new Timer();
timer.schele(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, long delay,long period);
delay延迟的时间,period为多久执行一次;
个人推荐用方法一,毕竟安卓系统自带的,并且安卓不适合加定时器,如果处理不当一直执行的话,要么卡,要么程序崩溃。还有发布版本可能审核不通过。

㈢ android 定时器如何让某天某个时间启动

Android中使用AlarmManager进行定时操作,现在需要启动多个定时器,但无论采用哪种方式后面的定时器都会将前面的定时器覆盖掉(Android系统版本2.1),只启动最后一个定时器,见代码 Java code// 方式一for (int i = 0; i < 10; i ++) { ... AlarmManager am = null; am = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE); ... Intent i = new Intent("xxx"); PendingIntent sender = PendingIntent.getBroadcast(context.getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT); ... am.setRepeating(...);}// 方式二AlarmManager am = null;am = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);for (int i = 0; i < 10; i ++) { ... Intent i = new Intent("xxx"); PendingIntent sender = PendingIntent.getBroadcast(context.getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT); ... am.setRepeating(...);} 请问如果要实现启动多个定时器应该怎么操作?Android系统的闹钟就是采用AlarmManager进行操作的,如何才能启动多个定时器呢?先谢谢大家了 ------解决方案-------------------- public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) requestCode 需要是唯一的

㈣ android 能不能在主线程定时操作

先给你提示一下。android中更新UI只能用UI线程,即主线程。 这样说吧 ui线程== 主线程。 1 想要在子线程中数据发生改变更新主线程的ui,可以通过消息机制,message和handler结合的方式,比较好用。 2 还可以 用特定的控件的方法,比如ListView的adapter中的notifydatachang().好像是这个方法 3 在view中还可以在数据变化后用invalidata()或者postInvalidata()这两个方法。 基本上就这几种常见的,希望可以帮你,大家都来讨论。

㈤ android 定时通知提醒

Android中有三种方式实现,当然三种方式都需要在服务器开启线程。

  1. 使用Android的倒计时,CountDownTimer这个类。

  2. 直接用java中的TaskTime定时器。

  3. 使用Handler发送延迟消息。

㈥ android 定时器暂停 可以用canel()方法 但我又想再次唤起 用什么方法

android 定时器是一个线程,线程canel 以后,只能重新new 并调用 start启动。

具体实现代码:

  1. 创建线程定时器同理

    Thread thread = new Thread();

    thread.start(); 启动线程

  2. 当线程或定时器停止后,需要重新new

    thread = new Thread();

thread.start(); 重新启动线程或定时器

㈦ 如何测试 Android 中的定时事件

犯基本错误
timer没问题问题Toast.makeText(e201301.this, "ABCD", Toast.LENGTH_LONG).show();

另外说没报错我太相信logcat面应该log除非logcat问题
另外Toast.makeText(e201301.this, "ABCD", Toast.LENGTH_LONG).show();
换其基本语句应该问题

问题主要用timer新线程希望新线程面直接UI线程显示东西于错

测试timerToast.makeText(e201301.this, "ABCD", Toast.LENGTH_LONG).show();
换Log.e("Timer运行", "ABCD");
看logcat面按照设定间断输句LOGTimer运行

依旧希望能UI线程显示参考线程间通信handle、Message或者简单利用AsyncTask等

㈧ android定时器alarmmanager和timer的区别

Java的Timer类可以用来计划需要循环执行的任务。
简单的说,一个Timer内部封装装了“一个Thread”和“一个TimerTask队列”,这个队列按照一定的方式将任务排队处理。封装的Thread在Timer的构造方法调用时被启动,这个Thread的run方法按照条件去循环这个TimerTask队列,然后调用TimerTask的run方法。

但是,如果CPU进入了休眠状态,那么这个thread将会因为失去CPU时间片而阻塞,从而造成我们需要的定时任务失效。上述定时任务失效的场景分析:假设定时任务的条件是到了时间xx:yy才能执行,但由于cpu休眠造成线程阻塞的关系,当前系统时间超过了这个时间,即便CPU从终端中恢复了,那么由于条件不满足,定时任务在这一次自然就失效了。

它需要用WakeLock让CPU 保持唤醒状态。这样会大量消耗手机电量,大大减短手机待机时间。这种方式不能满足需求。

AlarmManager是Android 系统封装的用于管理RTC的模块,RTC(Real Time Clock) 是一个独立的硬件时钟,可以在 CPU 休眠时正常运行,在预设的时间到达时,通过中断唤醒CPU。这意味着,如果我们用 AlarmManager 来定时执行任务,CPU 可以正常的休眠,只有在需要运行任务时醒来一段很短的时间。

㈨ android中的线程池 怎么用

Java的线程池对Android也是适用的
线程池的作用:
线程池作用就是限制系统中执行线程的数量。
根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程
排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程
池中有等待的工作线程,就可以开始运行了;否则进入等待队列。
为什么要用线程池:
1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。
2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)。

Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

1.newCachedThreadPool

/**
* 可以缓存线程池
*/
public static void Function1() {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
final int index = i;
try {
Thread.sleep(100); // 休眠时间越短创建的线程数越多
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
executorService.execute(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("active count = " + Thread.activeCount()
+ " index = " + index);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

打印结果
active count = 2 index = 0
active count = 3 index = 1
active count = 4 index = 2
active count = 5 index = 3
active count = 6 index = 4
active count = 7 index = 5
active count = 8 index = 6
active count = 9 index = 7
active count = 10 index = 8
active count = 11 index = 9
active count = 11 index = 10
active count = 11 index = 11
active count = 11 index = 12
active count = 11 index = 13
active count = 11 index = 14
active count = 11 index = 15
active count = 11 index = 16
active count = 11 index = 17
active count = 11 index = 18
active count = 11 index = 19
active count = 11 index = 20
active count = 11 index = 21
active count = 11 index = 22
active count = 11 index = 23
active count = 11 index = 24
active count = 11 index = 25
active count = 11 index = 26
active count = 11 index = 27
active count = 11 index = 28
active count = 11 index = 29
active count = 11 index = 30
active count = 11 index = 31
active count = 11 index = 32
active count = 11 index = 33
active count = 11 index = 34
active count = 11 index = 35
active count = 11 index = 36
active count = 11 index = 37
active count = 11 index = 38
active count = 11 index = 39
active count = 11 index = 40
active count = 11 index = 41
active count = 11 index = 42
active count = 11 index = 43
active count = 11 index = 44
active count = 11 index = 45
active count = 11 index = 46
active count = 11 index = 47
active count = 11 index = 48
active count = 10 index = 49
从打印消息来看开始线程数在增加,后来稳定,可以修改休眠时间,休眠时间越短创建的线程数就越多,因为前面的还没执行完,线程池中没有可以执行的就需要创建;如果把休眠时间加大,创建的线程数就会少

2.newFixedThreadPool 根据传入的参数创建线程数目
/**
* 定长线程池
*/
public static void Function2() {
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 30; i++) {
final int index = i;
executorService.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("index = " + index
+ " thread count = " + Thread.activeCount());
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

3.newScheledThreadPool
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

/**
* 定长线程池,可做延时
*/
public static void Function3() {
ScheledExecutorService executorService = Executors
.newScheledThreadPool(5);
executorService.schele(new Runnable() {

@Override
public void run() {
System.out.println("delay 3 seconds" + " thread count = "
+ Thread.activeCount());
}
}, 3, TimeUnit.SECONDS);
}

/**
* 定期执行,可以用来做定时器
*/
public static void Function4() {
ScheledExecutorService executorService = Executors
.newScheledThreadPool(3);
executorService.scheleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out
.println("delay 1 seconds, and excute every 3 seconds"
+ " thread count = " + Thread.activeCount());
}
}, 1, 3, TimeUnit.SECONDS);
}
打印结果
?

1
2
3
4
5
6
7
8
9

delay 1 seconds, and excute every 3 seconds thread count = 2
delay 1 seconds, and excute every 3 seconds thread count = 3
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4
delay 1 seconds, and excute every 3 seconds thread count = 4

4.newSingleThreadExecutor这个最简单
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

/**
* 单例线程
*/
public static void Function5() {
ExecutorService singleThreadExecutor = Executors
.newSingleThreadExecutor();
for (int i = 0; i < 5; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {

@Override
public void run() {
try {
System.out.println("index = " + index
+ " thread count = " + Thread.activeCount());
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

打印结果:
?

1
2
3
4
5

index = 0 thread count = 2
index = 1 thread count = 2
index = 2 thread count = 2
index = 3 thread count = 2
index = 4 thread count = 2

热点内容
买合资车选哪个品牌好配置高 发布:2025-05-19 18:01:51 浏览:49
pythonc回调 发布:2025-05-19 17:55:50 浏览:268
苹果如何把通讯录转移到安卓手机 发布:2025-05-19 17:49:13 浏览:255
开机设密码源码 发布:2025-05-19 17:47:20 浏览:885
死锁检测算法 发布:2025-05-19 17:45:17 浏览:109
sql查询列 发布:2025-05-19 17:38:16 浏览:279
安卓怎么录屏只录一点 发布:2025-05-19 17:12:39 浏览:522
甘肃移动服务密码在哪里 发布:2025-05-19 17:11:15 浏览:542
java内部类访问外部类方法 发布:2025-05-19 17:10:30 浏览:287
用解压造句 发布:2025-05-19 17:01:55 浏览:342