Your SlideShare is downloading. ×
[Curs Android] C10 - Threaduri & Servicii (IPW 2011)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

[Curs Android] C10 - Threaduri & Servicii (IPW 2011)

1,120
views

Published on

These slides are part of the Android Development Course which I taught to 10 students, while participating in the IP Workshop Summer School 2011. …

These slides are part of the Android Development Course which I taught to 10 students, while participating in the IP Workshop Summer School 2011.
http://www.scoaladevara.info/2011/?page_id=39

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,120
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android Threaduri, Servicii, AIDL Vlad PETRE vlad@vladpetre.com http://twitter.com/vladpetre88 http://facebook.com/vladpetre88 http://www.linkedin.com/in/vladpetre881 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 2. Intai, cateva informatii preliminare2 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 3. Ierarhia de memorii Creste marimea Creste viteza3 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 4. Multitasking4 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 5. Starile proceselor5 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 6. Schimbarea de context • Schimbarea programului curent de pe procesor – Salvarea registrilor – Salvarea setarilor de memorie6 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 7. Fire de executie (Threads) • Forma de impartire a programului – Mai multe cai de procesare – Partajarea memoriei intre acestea • Variabilele sunt comune • Tipuri de fire de executie – User – Kernel7 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 8. Implementarea in Java • Prin extinderea clasei Thread – Si suprascrierea metodei run() • Prin implementarea interfetei Runnable – Si implmentarea metodei run()8 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 9. Metoda 1: extinderea clasei Thread9 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 10. Clasa Thread class FirExecutie extends Thread { // … constructori, functii, etc. public void run() { // setul de instructiuni ce trebuie sa fie // executat de firul de executie } }10 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 11. Utilizarea clasei FirExecutie class FirExecutie extends Thread { // … constructori, functii, etc. public void run() { // setul de instructiuni ce trebuie sa fie // executat de firul de executie } } FirExecutie fe = new FirExecutie(…); fe.start(); <-nu apelam direct metoda run()!!!11 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 12. Metoda 2: implementarea interfetei Runnable12 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 13. Interfata Runnable class FirExecutie implements Runnable { // … constructori, functii, etc. public void run() { // codul firului de executie separat } }13 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 14. Utilizarea clasei FirExecutie class FirExecutie implements Runnable { // … constructori, functii, etc. public void run() { // codul firului de executie separat } } Thread fir = new Thread(new FirExecutie()); fir.start(); <-nu apelam direct metoda run()!!!14 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 15. Diferenta intre Runnable si Thread15 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 16. Diferenta intre Runnable si Thread Runnable Thread •Interfata •Clasa •Obiectul poate extinde •Obiectul trebuie sa extinda orice alta clasa clasa Thread •Doar implmenteaza •Mai putin flexibil interfata Runnable •Cod mai simplu •Mai flexibil •Cod putin mai complicat16 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 17. Oprirea unui fir de executie Doar cand se termina de executat setul de instructiuni din metoda run()17 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 18. Revenim la Android18 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 19. Procesarea în Activități • Procesarea in activitati are loc in: – Functiile activitatii • onCreate(…) • onStart(…) • … – Funcțiile observatorilor • onClick(…) • Procesare putina!!! – Trebuie sa evitam starea de Not Responding • Solutia – Fire de executie – Servicii – AsyncTask19 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 20. Servicii • Componenta Android • Speciala pentru procesare • Ruleaza in fundal (background) • Prioritate mai mica • Mai stabil (in timp) • Un serviciu ruleaza in threadul principal (threadul in care ruleaza si activitatile) in cadrul procesului aplicatiei sale. Componenta Service NU isi creaza propriul sau thread daca noi nu explicitam acest lucru.20 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 21. Evenimente 21 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 22. Implementarea serviciilor • Extinde clasa Service – Serviciu simplu • void onCreate(); • int onStartCommand(Intent intent, int flags, int startId); • void onDestroy(); – Folosind AIDL • void onBind(Intent intent); • void onUnbind();22 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 23. Servicii simple23 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 24. Implementarea unui serviciu 1.Extinderea clasei Service – Implementarea metodelor – Crearea unor fire de execuție 2.Declararea componentei Service in Manifest 3.Pornirea/oprirea – startService(…) – stopService(…)24 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 25. Exemplu • Afiseaza un numar prim pe secunda – Serviciul nu porneste automat – Serviciul nu se opreste automat25 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 26. Numere Prime - Serviciu26 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 27. Numere Prime–Thread efectiv 27 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 28. Pornirea serviciului • Intent starter = new Intent(context, ClasaServiciu.class); • context.startService(starter);28 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 29. Oprirea serviciului • Doua metode: – Intent intent = new Intent(context, ClasaServiciu.class); context.stopService(intent); – stopSelf();29 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 30. Servicii cu AIDL30 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 31. AIDL • Android Interface Definition Language – RPC pentru Android • Conexiune intre activitate si serviciu31 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 32. Remote Procedure Call • RPC este un mod de comunicare intre diferite procese, ce permit ca un program sa apeleze subrutine ale altui program.32 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 33. AIDL • Seamana cu Java • Declaratii identice • Tipuri de variabile limitate • Primare (int, long, float, double, char, boolean) – String – List* – Map* – Tipuri special construite * Trebuie sa contina doar tipuri pentru AIDL33 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 34. Exemplu: IServiciuFilme.aidl package pdm.filme; interface IServiciuFilme { Int numarFilme(); String titluFilm(intnumar); String regizorFilm(intnumar); }34 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 35. AIDL => clasa • IServiciuFilme.aidl – IServiciuFilme.Stub • Clasa abstracta cu functiile din Interfata35 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 36. ListaFilme - Serviciu36 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 37. ListaFilme – serviciul efectiv 37 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 38. Conectare folosind AIDL38 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 39. Conectare folosind AIDL39 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 40. Bibliografie • http://pdm.ipworkshop.ro • http://download.oracle.com/javase/tutorial/essential/concurrency/ • http://developer.android.com/guide/topics/fundamentals/services.html • http://developer.android.com/guide/developing/tools/aidl.html • http://developer.android.com/reference/android/app/Service.html40 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea
  • 41. Întrebări?41 Scoala de Vara IP Workshop 2011 – Calimanesti Valcea