當前位置:首頁 » 安卓系統 » 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 20:27:04 瀏覽:164
瘋狂android講義光碟 發布:2025-05-19 20:12:31 瀏覽:152
安卓手機怎麼下載圈點 發布:2025-05-19 20:08:11 瀏覽:473
文件夾粉碎不了 發布:2025-05-19 20:05:41 瀏覽:243
安卓怎麼把軟體放進全局 發布:2025-05-19 20:03:55 瀏覽:688
安卓手機如何看最真實的型號 發布:2025-05-19 19:58:59 瀏覽:11
U盤超級加密2008 發布:2025-05-19 19:44:32 瀏覽:455
燈帶編程軟體 發布:2025-05-19 19:32:30 瀏覽:288
如何判斷伺服器被多少人訪問 發布:2025-05-19 19:27:45 瀏覽:126
編程stata 發布:2025-05-19 19:12:18 瀏覽:517