【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android延遲實(shí)現(xiàn)的方法有哪些_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論