




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些
1.實(shí)現(xiàn)延遲的幾種方法?1.java.util.Timer類(lèi)的:public
void
schedule(TimerTask
task,
long
delay)
{
if
(delay
<
0)
throw
new
IllegalArgumentException("Negative
delay.");
sched(task,
System.currentTimeMillis()+delay,
0);
}2.android.os.Handler類(lèi):public
final
boolean
postDelayed(Runnable
r,
long
delayMillis)
{
return
sendMessageDelayed(getPostMessage(r),
delayMillis);
}3.android.app.AlarmManager類(lèi):
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
public
void
set(@AlarmType
int
type,
long
triggerAtMillis,
long
windowMillis,
long
intervalMillis,
OnAlarmListener
listener,
Handler
targetHandler,
WorkSource
workSource)
{
setImpl(type,
triggerAtMillis,
windowMillis,
intervalMillis,
0,
null,
listener,
null,
targetHandler,
workSource,
null);
}4.Thread.sleep()然后在一定時(shí)間之后再執(zhí)行想執(zhí)行的代碼:new
Thread(new
Runnable(){
Thead.sleep(4*1000);
doTask();
}).start()2.他們的各自的實(shí)現(xiàn)原理?1.Timer的實(shí)現(xiàn),是通過(guò)內(nèi)部開(kāi)啟一個(gè)TimerThread:private
void
mainLoop()
{
while
(true)
{
try
{
TimerTask
task;
boolean
taskFired;
synchronized(queue)
{
//
Wait
for
queue
to
become
non-empty
while
(queue.isEmpty()
&&
newTasksMayBeScheduled)
queue.wait();
if
(queue.isEmpty())
break;
//
Queue
is
empty
and
will
forever
remain;
die
//
Queue
nonempty;
look
at
first
evt
and
do
the
right
thing
long
currentTime,
executionTime;
task
=
queue.getMin();
synchronized(task.lock)
{
if
(task.state
==
TimerTask.CANCELLED)
{
queue.removeMin();
continue;
//
No
action
required,
poll
queue
again
}
currentTime
=
System.currentTimeMillis();
executionTime
=
task.nextExecutionTime;
if
(taskFired
=
(executionTime<=currentTime))
{
if
(task.period
==
0)
{
//
Non-repeating,
remove
queue.removeMin();
task.state
=
TimerTask.EXECUTED;
}
else
{
//
Repeating
task,
reschedule
queue.rescheduleMin(
task.period<0
?
currentTime
-
task.period
:
executionTime
+
task.period);
}
}
}
if
(!taskFired)
//
Task
hasn't
yet
fired;
wait
queue.wait(executionTime
-
currentTime);
}
if
(taskFired)
//
Task
fired;
run
it,
holding
no
locks
task.run();
}
catch(InterruptedException
e)
{
}
}
}2.Handler的postDelay是通過(guò)設(shè)置Message的when為delay的時(shí)間,我們知道當(dāng)我們的應(yīng)用開(kāi)啟的時(shí)候,會(huì)同步開(kāi)啟Looper.loop()方法循環(huán)的,不停的通過(guò)MeassgeQueue的next方法:Message
next()
{
int
nextPollTimeoutMillis
=
0;
for
(;;)
{
if
(nextPollTimeoutMillis
!=
0)
{
Binder.flushPendingCommands();
}
nativePollOnce(ptr,
nextPollTimeoutMillis);
synchronized
(this)
{
//
Try
to
retrieve
the
next
message.
Return
if
found.
final
long
now
=
SystemClock.uptimeMillis();
Message
prevMsg
=
null;
Message
msg
=
mMessages;
if
(msg
!=
null
&&
msg.target
==
null)
{
//
Stalled
by
a
barrier.
Find
the
next
asynchronous
message
in
the
queue.
do
{
prevMsg
=
msg;
msg
=
msg.next;
}
while
(msg
!=
null
&&
!msg.isAsynchronous());
}
if
(msg
!=
null)
{
if
(now
<
msg.when)
{
//
Next
message
is
not
ready.
Set
a
timeout
to
wake
up
when
it
is
ready.
nextPollTimeoutMillis
=
(int)
Math.min(msg.when
-
now,
Integer.MAX_VALUE);
}
else
{
//
Got
a
message.
mBlocked
=
false;
if
(prevMsg
!=
null)
{
prevMsg.next
=
msg.next;
}
else
{
mMessages
=
msg.next;
}
msg.next
=
null;
if
(DEBUG)
Log.v(TAG,
"Returning
message:
"
+
msg);
msg.markInUse();
return
msg;
}
}
else
{
//
No
more
messages.
nextPollTimeoutMillis
=
-1;
}
}
}boolean
enqueueMessage(Message
msg,
long
when)
{
if
(msg.target
==
null)
{
throw
new
IllegalArgumentException("Message
must
have
a
target.");
}
if
(msg.isInUse())
{
throw
new
IllegalStateException(msg
+
"
This
message
is
already
in
use.");
}
synchronized
(this)
{
if
(mQuitting)
{
IllegalStateException
e
=
new
IllegalStateException(
msg.target
+
"
sending
message
to
a
Handler
on
a
dead
thread");
Log.w(TAG,
e.getMessage(),
e);
msg.recycle();
return
false;
}
msg.markInUse();
msg.when
=
when;
Message
p
=
mMessages;
boolean
needWake;
if
(p
==
null
||
when
==
0
||
when
<
p.when)
{
msg.next
=
p;
mMessages
=
msg;
needWake
=
mBlocked;
}
else
{
needWake
=
mBlocked
&&
p.target
==
null
&&
msg.isAsynchronous();
Message
prev;
for
(;;)
{
prev
=
p;
p
=
p.next;
if
(p
==
null
||
when
<
p.when)
{
break;
}
if
(needWake
&&
p.isAsynchronous())
{
needWake
=
false;
}
}
msg.next
=
p;
//
invariant:
p
==
prev.next
prev.next
=
msg;
}
mQuitting
is
false.
if
(needWake)
{
nativeWake(mPtr);
}
}
return
true;
}3.AlarmManager的延遲的實(shí)現(xiàn)原理,是通過(guò)一個(gè)AlarmManager的set方法:IAlarmManager
mService.set(mPackageName,
type,
triggerAtMillis,
windowMillis,
intervalMillis,
flags,
operation,
recipientWrapper,
listenerTag,
workSource,
alarmClock);
private
final
IBinder
mService
=
new
IAlarmManager.Stub()
{
@Override
public
void
set(String
callingPackage,
int
type,
long
triggerAtTime,
long
windowLength,
long
interval,
int
flags,
PendingIntent
operation,
IAlarmListener
directReceiver,
String
listenerTag,
WorkSource
workSource,
AlarmManager.AlarmClockInfo
alarmClock)
{
final
int
callingUid
=
Binder.getCallingUid();
if
(interval
!=
0)
{
if
(directReceiver
!=
null)
{
throw
new
IllegalArgumentException("Repeating
alarms
cannot
use
AlarmReceivers");
}
}
if
(workSource
!=
null)
{
getContext().enforcePermission(
android.Manifest.permission.UPDATE_DEVICE_STATS,
Binder.getCallingPid(),
callingUid,
"AlarmManager.set");
}
//
No
incoming
callers
can
request
either
WAKE_FROM_IDLE
or
//
ALLOW_WHILE_IDLE_UNRESTRICTED
--
we
will
apply
those
later
as
appropriate.
flags
&=
~(AlarmManager.FLAG_WAKE_FROM_IDLE
|
AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED);
//
Only
the
system
can
use
FLAG_IDLE_UNTIL
--
this
is
used
to
tell
the
alarm
//
manager
when
to
come
out
of
idle
mode,
which
is
only
for
DeviceIdleController.
if
(callingUid
!=
Process.SYSTEM_UID)
{
flags
&=
~AlarmManager.FLAG_IDLE_UNTIL;
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度診所執(zhí)業(yè)醫(yī)師醫(yī)療風(fēng)險(xiǎn)防控聘用合同
- 二零二五年度手車(chē)轉(zhuǎn)讓與綠色出行推廣合同
- 二零二五年度投資分紅股收益分配協(xié)議
- 二零二五年度汽車(chē)展覽會(huì)參展商展位電力合同
- 2025年度道路破碎修復(fù)與再生利用合同
- 2025年度青貯收割作業(yè)與農(nóng)業(yè)物聯(lián)網(wǎng)平臺(tái)合作協(xié)議
- 二零二五年度口腔診所醫(yī)生培訓(xùn)與薪酬管理合同
- 二零二五年度商業(yè)秘密保護(hù)與員工保密義務(wù)合同
- 二零二五年度多功能辦公場(chǎng)所租賃服務(wù)協(xié)議
- 2025年度蔬菜大棚承包與品牌授權(quán)合作協(xié)議
- 《企業(yè)員工培訓(xùn)國(guó)內(nèi)外文獻(xiàn)綜述》4800字
- 麻醉藥品與精神藥品不良反應(yīng)的防治 (1) - 副本課件
- 車(chē)輛保險(xiǎn)登記臺(tái)賬參考模板范本
- 三年級(jí)下冊(cè)數(shù)學(xué)教案-速度、時(shí)間和路程 滬教版
- 礦山道路施工組織設(shè)計(jì)方案
- 正弦函數(shù)的圖像與性質(zhì)優(yōu)秀課件
- 山東省任氏宗親分布村落
- 北師大版小學(xué)數(shù)學(xué)五年級(jí)下冊(cè)《有趣的折疊》說(shuō)課稿
- 陜西省建設(shè)工程長(zhǎng)安杯獎(jiǎng)省優(yōu)質(zhì)工程結(jié)構(gòu)備案和復(fù)查的要求
- 典型示功圖分析(全)
- 水生觀賞動(dòng)物鑒賞與維護(hù)課程
評(píng)論
0/150
提交評(píng)論