當前位置:首頁 » 安卓系統 » android線程休眠

android線程休眠

發布時間: 2022-07-04 19:22:51

❶ android 子線程訪問網路超時怎麼辦

Android開發中經常需要調用線程訪問網路,而手機的網路信號經常斷斷續續,容易出現網路超時的情況,這種情況下後台線程往往得不到關閉,浪費系統資源。
在下面的例子中使用了java 中的Timer類,對線程進行了約束,如果線程在一定時間內為響應則終止該線程。
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final int TIME_OUT = 0;
private static final int SUCCESS = 1;
// 超時的時限為5秒
private static final int TIME_LIMIT = 5000;
ProgressDialog proDialog;
Timer timer;
Thread thread;
Button btn1, btn2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
// 測試未超時的線程,為btn1綁定事件
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
proDialog = ProgressDialog.show(MainActivity.this, "提示",
"線程運行中");
// 匿名內部線程
thread = new Thread() {
@Override
public void run() {
while (true) {
try {
//線程休眠時間,超時
sleep(10000);
} catch (InterruptedException e) {
break;
}
}

}
};
thread.start();
// 設定定時器
timer = new Timer();
timer.schele(new TimerTask() {
@Override
public void run() {
sendTimeOutMsg();
}
}, TIME_LIMIT);

}
});
// 測試超時的線程,為btn2綁定事件
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

proDialog = ProgressDialog.show(MainActivity.this, "提示",
"線程運行中");
// 匿名內部線程
thread = new Thread() {
public void run() {
try {
// 線程休眠時間,未超時
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msgSuc = new Message();
msgSuc.what = SUCCESS;
myHandler.sendMessage(msgSuc);
}
};
thread.start();
// 設定定時器
timer = new Timer();
timer.schele(new TimerTask() {
@Override
public void run() {
sendTimeOutMsg();
}
}, TIME_LIMIT);
}
});
}
// 接收消息的Handler
final Handler myHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case TIME_OUT:
//打斷線程
thread.interrupt();
proDialog.dismiss();
Toast.makeText(MainActivity.this, "線程超時", Toast.LENGTH_SHORT)
.show();
break;
case SUCCESS:
//取消定時器
timer.cancel();
proDialog.dismiss();
Toast.makeText(MainActivity.this, "線程運行完成", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
};
};
//向handler發送超時信息
private void sendTimeOutMsg() {
Message timeOutMsg = new Message();
timeOutMsg.what = TIME_OUT;
myHandler.sendMessage(timeOutMsg);
}

}

❷ android中怎麼讓線程長時間休眠線程休眠最長的安全時間是多少

設定一個Timer對象,時間到後觸發事件再給handler發消息處理就行,處理完重置timer的時間進入下一個循環即可。隨便用wait之類的土辦法的話很容易被系統判定為進程掛死然後被kill掉的。

❸ android系統睡眠狀態如何喚醒線程和廣播

不能!
(不能手動喚醒,因為肯定需要點亮屏幕(手動點亮屏幕),所以並不是真睡眠狀態)。
只能提前設置,比如鬧鍾,具體到「廣播」即收音機,那麼只建議使用第三方程序,如「蜻蜓FM」,就像鬧鍾可以定時自動開啟。

如果是自己造,相當於重新編個程序出來,需要掌握大量專業性的東西,得不償失

❹ 如何讓android系統禁止休眠

在開發Android程序時,有時候在程序運行的時候,不能讓系統休眠,否則有一些運行會停止,因此我們需要設置禁止休眠,有兩種方式:一種是添加許可權,別一種是代碼中設置,建議使用第一種方式,這樣,在安裝程序的時候會進行提醒:
第一種方式:
在Manifest.xml文件裡面用user-permission聲明
名稱為:android.permission.WAKE_LOCK

第二種方式:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
把這段代碼加在setContentView(R.layout.main)之前即可

❺ java代碼怎麼控制android休眠和喚醒

喚醒:android.intent.action.SCREEN_ON (代碼)

休眠:android.intent.action.SCREEN_OFF (代碼)

android系統一段時間沒有操作,

屏幕(screen)將從高亮(bright)變為暗淡(dim),如果再過段時間還是沒有操作,屏幕(screen)從暗淡(dim)變為關閉(off).這時,系統將進入休眠.

而對於某些需要保持系統喚醒甚至屏幕喚醒的應用(比如視頻播放器和音樂播放器)來說,就必須要有一個機制,使得系統不進入休眠狀態,設置保持屏幕亮屏狀態.

wakelock即用來實現以上目的

接下來對每一個模塊具體分析:


powermanager

對應文件是android/frameworks/base/core/java/android/os/PowerManager.java


在Android中應用程序並不是直接同PowerManagerService交互的,而是通過PowerManager間接地與PowerManagerService打交道。

此文件定義了一個powermanager類.

主要實現了

1,wakelock的申請與釋放

public WakeLock newWakeLock(int flags, String tag)

2,系統延時進入休眠

public void userActivity(long when, boolean noChangeLights)

3,系統強制休眠

public void goToSleep(long time)

4,屏幕亮度設置

public void setBacklightBrightness(int brightness)

5,屏幕狀態查詢

public boolean isScreenOn()

6,系統重啟

public void reboot(String reason)


細節

wakelock的申請與釋放

{@samplecode

*PowerManager pm = (PowerManager)mContext.getSystemService(

* Context.POWER_SERVICE);

*PowerManager.WakeLock wl = pm.newWakeLock(

* PowerManager.SCREEN_DIM_WAKE_LOCK

* | PowerManager.ON_AFTER_RELEASE,

* TAG);

*wl.acquire();

* // ...

*wl.release();

一共有如下幾個flag來進行不一樣的喚醒方式.可以根據需要設置

Flag Value CPU Screen Keyboard

PARTIAL_WAKE_LOCK On* can-off Off

SCREEN_DIM_WAKE_LOCK On Dim Off

PROXIMITY_SCREEN_OFF_WAKE_LOCK on 距離感測器時關閉 off

SCREEN_BRIGHT_WAKE_LOCK On Bright Off

FULL_WAKE_LOCK On Bright Bright


ACQUIRE_CAUSES_WAKEUP 確保wakelock,主要用於視頻播放器

ON_AFTER_RELEASE = 0x20000000 release後倒計時,關閉屏幕

...


userActivity的作用:

使系統從其他狀態進入全部打開狀態,比如從暗屏(dim)切換到亮屏,並重置倒計時計數器

❻ android系統休眠關閉了哪些功能

Android的幾種不同的休眠模式
如果不進行特別的設置,Android會在一定時間後屏幕變暗,在屏幕變暗後一定時間內,約幾分鍾,CPU也會休眠,大多數的程序都會停止運行,從而節省電量。但你可以在代碼中通過對Powmanager API的調用來設置不同的休眠模式。
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
如上表,最高等級的休眠是屏幕,鍵盤等,cpu都全部休眠。可以設置不同的模式,讓其產生不同的休眠,比如讓cpu保持運行。

❼ android 子線程長時間休眠會不會被殺死

會, android 應用程序層使用java 語言, java 有gc 自動回收機制, 長時間沒有使用的資源會自動回收,所以說一般情況下java 不用開發者自己去釋放內存。

❽ 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

❾ 如何控制android 設備休眠 ,禁用休眠,設置等待時間

到設置~顯示~休眠下選擇合適的時間甚至取消休眠。

❿ android 如何讓手機進入休眠狀態,又如何喚

1.WakeLock主要代碼如下:

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());//持有喚醒鎖
wakeLock.setReferenceCounted(false);
wakeLock.acquire(30*1000);//30s亮屏
wakeLock.release();//釋放鎖,滅屏

2.FLAG_KEEP_SCREEN_ON代碼如下
可使用 FLAG_KEEP_SCREEN_ON 替換WakeLock的方式.
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//亮屏
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//滅屏

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:633
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:362
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:78
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:303
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:789
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:339
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:206
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:806
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:360
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:586