timertaskandroid
Ⅰ 安卓 timertask是多線程的嗎
模擬一個人的生活起居就行了,, 從早7點 到 晚10點 每個小時到整點時都開一個線程 比如 早上7點 開啟一個timer,執行對應的TimerTask ,這個task裡面描述你7點起床,和起床後乾的事, 到了8點,7點線程休眠,喚醒8點的,然後描述8點里你乾的事 , 一直這么寫下去,,,邏輯大概這樣,具體寫多少行 就看你自己了。。
Ⅱ android的timertask
講講需求、邏輯、原來的代碼是如何的
Ⅲ android timertask 在內存不足的情況下會被殺死嗎
通常在一部Android手機里同時運行著多個應用(app),每個app對應一個系統進程,當系統需要更多的資源(如內存)而空閑資源不足時,Android系統就會選擇殺掉一些「低優先順序」的進程以便釋放所需資源。Android系統是如何確定進程優先順序的高低的呢?如果一個app正在與用戶交互,那麼它所在的進程具有最高優先順序;其次,如果一個app是可見的,例如被一個對話框部分遮擋,它所在進程具有第二高的優先順序;再次,如果app當前是不可見的,也就是被切換到了後台,則它所在進程具有第三高的優先順序;這里要補充一點,如果這個後台app啟動了一個service,則它比一般的後台app優先順序高一些。最後,如果一個進程里沒有包含任何app,這個進程的優先順序是最低的。
Ⅳ android 使用timertask和alarmmanager哪個好點
在消息的獲取上是選擇輪詢還是推送得根據實際的業務需要來技術選型,例如對消息實時性比較高的需求,比如微博新通知或新聞等那就最好是用推送了。但如果只是一般的消息檢測比如更新檢查,可能是半個小時或一個小時一次,那用輪詢也是一個不錯的選擇,因為不需要額外搭建推送伺服器,不用額外配置推送服務。另外推送現在一般以維持長連接的方式實現,在手機客戶端也會耗費一定的電量。今天就介紹一個在Android上實現輪詢機制的方法——使用AlarmManager
AlarmManager在Android中主要用來定時處理一個事件或是定期處理一個事件,比如鬧鍾應用就是使用AlarmManager來實現的,我們今天要使用AlarmManager的定期執行功能來實現輪詢的功能。對於定期執行任務也可以用Timer和TimerTask來實現,也可以開一個Service在Thread裡面以while循環來實現。但最好的方案還是選用AlarmManager,這里涉及一個Android系統鎖的機制,即系統在檢測到一段時間沒有活躍以後,會關閉一些不必要的服務來減少資源和電量消耗。使用Timer和Service來實現的話很可能出現的情況就是屏幕熄滅後一段時間,服務就被停止了,當然輪詢也就被停止了。這個大家可以實驗一下,之前我寫過一篇文章也介紹了一種保持後台喚醒的機制《使用WakeLock使Android應用程序保持後台喚醒》,感興趣的可以看看。那麼接下來就開始使用AlarmManager+Service+Thread來實現我們的輪詢服務吧!
一、新建輪詢工具類PollingUtils.java
[java]view plain
publicclass PollingUtils {
//開啟輪詢服務
publicstaticvoid startPollingService(Context context, int seconds, Class<?> cls,String action) {
//獲取AlarmManager系統服務
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
//包裝需要執行Service的Intent
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
//觸發服務的起始時間
long triggerAtTime = SystemClock.elapsedRealtime();
//使用AlarmManger的setRepeating方法設置定期執行的時間間隔(seconds秒)和需要執行的Service
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
seconds * 1000, pendingIntent);
}
//停止輪詢服務
publicstaticvoid stopPollingService(Context context, Class<?> cls,String action) {
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
//取消正在執行的服務
manager.cancel(pendingIntent);
}
}
二、構建輪詢任務執行PollingService.java
[java]view plain
publicclass PollingService extends Service {
publicstaticfinal String ACTION = "com.ryantang.service.PollingService";
private Notification mNotification;
private NotificationManager mManager;
@Override
public IBinder onBind(Intent intent) {
returnnull;
}
@Override
publicvoid onCreate() {
initNotifiManager();
}
@Override
publicvoid onStart(Intent intent, int startId) {
new PollingThread().start();
}
//初始化通知欄配置
privatevoid initNotifiManager() {
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
mNotification = new Notification();
mNotification.icon = icon;
mNotification.tickerText = "New Message";
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
}
//彈出Notification
privatevoid showNotification() {
mNotification.when = System.currentTimeMillis();
//Navigator to the new activity when click the notification title
Intent i = new Intent(this, MessageActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
Intent.FLAG_ACTIVITY_NEW_TASK);
mNotification.setLatestEventInfo(this,
getResources().getString(R.string.app_name), "You have new message!", pendingIntent);
mManager.notify(0, mNotification);
}
/**
* Polling thread
* 模擬向Server輪詢的非同步線程
* @Author Ryan
* @Create 2013-7-13 上午10:18:34
*/
int count = 0;
class PollingThread extends Thread {
@Override
publicvoid run() {
System.out.println("Polling...");
count ++;
//當計數能被5整除時彈出通知
if (count % 5 == 0) {
showNotification();
System.out.println("New message!");
}
}
}
@Override
publicvoid onDestroy() {
super.onDestroy();
System.out.println("Service:onDestroy");
}
}
三、在MainActivity.java中開啟和停止PollingService
[java]view plain
publicclass MainActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start polling service
System.out.println("Start polling service...");
PollingUtils.startPollingService(this, 5, PollingService.class, PollingService.ACTION);
}
@Override
protectedvoid onDestroy() {
super.onDestroy();
//Stop polling service
System.out.println("Stop polling service...");
PollingUtils.stopPollingService(this, PollingService.class, PollingService.ACTION);
}
}
Ⅳ android程序耗時,timerTask,timer,handler問題
什麼耗時40s還是20s?不是很明白
你弄清楚Timer.schele的意思就能明白了呢。
timer.schele(timerTask, seconds * 1000, 1000); //當前是執行一個循環的任務,timerTask是要被執行的任務, seconds * 1000ms後執行第一次timerTask,然後每隔1000ms執行一次timerTask。
Ⅵ android TimerTask()中加入了一個自定義的方法,定時去重復執行,為什麼執行時軟體會停止工作
需要通過Handler來做這件事. 你這樣寫會被安卓認為有可能阻礙UI線程造成卡機.
Ⅶ android 如何判斷timertask是否被調用
用了 cancel() 方法後,對象可能已釋放了,
如果再次用 timer,task, 需要重新新建對象
timer = new Timer() 等。試一試。
Ⅷ android TimerTask與Timer的問題
用了 cancel() 方法後,對象可能已釋放了,
如果再次用 timer,task, 需要重新新建對象
timer = new Timer() 等。試一試。
Ⅸ android用TimerTask做個計時器,第二次調用總是變得很快怎麼解決
第二次調用,你又啟動了一條線程,這個時候是兩個線程在跑
肯定變快了你應該把上一次的線程先取消掉
Ⅹ Timer、TimerTask的簡單應用及如何解決多次調度相同任務。
1、Timer類用來幹嘛滴?Timer是提供以指定的時間間隔執行某方法的這樣一種機制,即如果想要實現一個定時發送數據,比如每隔3s中發送一次心跳報文,或者執行某個指定的方法,都可以考慮用Timer類來實現,不過要提出的是Timer類一邊用來做一些比較簡單又不耗時間的操作。據說是因為它執行的任務仍然在主線程裡面(不確定,望牛人指出)。2、Timer類最常用的方法:cancel() //取消當然任務schele(TimerTask task, long delay) //延時一段時間執行task任務schele(TimerTask task, long delay, long period)//延時delay時間後,執行task任務,以後每隔peroid時間執行一次task任務。3、如何用?在Java中如何使用,代碼如下:public class TimerTest{public static void main(String[] args){timerTest();}private static void timerTest(){final Timer timer = new Timer();TimerTask task = new TimerTask(){int i = 0;public void run(){System.out.println("次數====" + i++);if(i == 10){i = 0 ;timer.cancel();// timerTest();}}};timer.schele(task, 1000, 1000);}}註: 注釋掉藍色字眼的timerTest();是用來解決多次調度執行相同任務實現滴。就如實現從0到10輸出後,又要從0開始輸出,這兩次調度。就要用重新調用此方法來實現。Android裡面,代碼如下:public class TimerTestAndroidActivity extends Activity{Handler handler;TextView textView;Timer timer ;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);textView = (TextView) findViewById(R.id.textView1);playTimer();handler = new Handler(){int i = 0;public void handleMessage(Message msg){i = msg.what;textView.setText("The current number is " + i);if (i == 5){timer.cancel();playTimer();}}};}private void playTimer(){timer= new Timer();timer.schele(new TimerTask(){ int i;public void run(){Message msg = new Message();msg.what = i++;handler.sendMessage(msg);}}, 1000, 1000); }}
