começando com android
DESCRIPTION
Palestra apresentada por Ivan de Aguirre no AndroidDay 2013.TRANSCRIPT
Começando com Android
Ivan de Aguirre@IvAguirre
Agenda• Parte1:
o Fragmentação.o Aplicações Nativas ou Híbridas?o Interfaceo Android 4.3
• Parte2:o Como o Android gerencia recursoso Threadingo Memory leaks
Fragmentação
Fragmentação - níveis de API
4.0.3 - 4.0.4 API 15
Ice Cream Sandwich
Fragmentação - níveis de API
4.1 - API 164.2 - API 174.3 - API 18
Jelly Bean
Fragmentação - níveis de API
android:minSDKVersion
Fragmentação - níveis de API
http://developer.android.com/about/dashboards/index.html#Platform
Fragmentação - níveis de API
Google Play Developer Console
Fragmentação - níveis de API
android:minSDKVersion=”10”
Fragmentação - níveis de API
android:minSDKVersion=”10”
Android Support Library
Fragmentação - tela
OpenSignalhttp://opensignal.com/reports/
fragmentation-2013/
http://opensignal.com/reports/fragmentation-2013/
Fragmentação - tela - Screen Size
small, normal, large, xlarge, sw600dp, w1024dp, h720dp, ...
Fragmentação - tela - Density
dpi = dots per inch (ldpi, mdpi, hdpi, xhdpi)
dp = density-independent pixelsp = scale-independent pixel
wrap_contentmatch_parent
<Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="100dp" android:text="@android:string/ok" />
Aplicações Nativas X Híbridas
Android, iOS, Windows Phone...
Aplicações Nativas X Híbridas
Android, iOS, Windows Phone...
Custo-benefício de três aplicações > Custo de uma aplicação híbrida ?
Aplicações Nativas X Híbridas
Aplicações Nativas
X
Aplicações Híbridas:
+ JavaScriptcódigo nativo +
Aplicações Nativas X Híbridas
“You can write amazing Web 2.0 and Ajax apps that look exactly and behave exactly like apps on the iPhone. (...) And guess what? There’s no SDK that you need! You’ve got everything you need if you know how to write apps using the most modern web standards to write amazing apps for the iPhone today. So developers, we think we’ve got a very sweet story for you. You can begin building your iPhone apps today.”
Steve Jobs, 2007
Aplicações Nativas X Híbridas
“You can write amazing Web 2.0 and Ajax apps that look exactly and behave exactly like apps on the iPhone. (...) And guess what? There’s no SDK that you need! You’ve got everything you need if you know how to write apps using the most modern web standards to write amazing apps for the iPhone today. So developers, we think we’ve got a very sweet story for you. You can begin building your iPhone apps today.”
Steve Jobs, 2007
Só que não…
Aplicações Nativas X HíbridasMartin Fowler, Developing Software for Multiple Mobile
Devices.http://martinfowler.com/articles/multiMobile/
Aplicações Nativas X HíbridasMartin Fowler, Developing Software for Multiple Mobile
Devices.http://martinfowler.com/articles/multiMobile/
“but cross-platform failed for desktop, so why should we
expect it to succeed for mobile?”
Aplicações Nativas X HíbridasMartin Fowler, Developing Software for Multiple Mobile
Devices.http://martinfowler.com/articles/multiMobile/
“To translate to all platforms, you can only use what all platforms support - which means you can only use a subset of each platform's
behavior.”
“but cross-platform failed for desktop, so why should we
expect it to succeed for mobile?”
Aplicações Nativas X HíbridasMartin Fowler, Developing Software for Multiple Mobile
Devices.http://martinfowler.com/articles/multiMobile/
“To translate to all platforms, you can only use what all platforms support - which means you can only use a subset of each platform's
behavior.”
“but cross-platform failed for desktop, so why should we
expect it to succeed for mobile?”
“...UI translation leads you to... The Uncanny Valley”
Aplicações Nativas X Híbridas
“Javascript is too slow for mobile app use in 2013 (e.g., for photo editing
etc.). It’s slower than native code by about 5...“
http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/
Aplicações Nativas X Híbridas
Se sua aplicação mobile for um diferencial para o seu negócio considere fazer
nativa.
Aplicações Nativas X Híbridas
Que tal site web otimizado para mobile, que execute no Browser?
http://developer.android.com/design
Interfaces
• Bluetooth Low Energy (Smart Ready).
• Media DRM.
• Interfaces Java para OpenGL ES
3.0.
• Android key store.
• Hardware credential storage.
Android 4.3
Parte 2
Algumas boas práticas.Sim... vamos ver um pouco
de código agora :)
Gerência de Recursos
0 100%
A B C
Gerência de Recursos
0 100%
B C
Gerência de Recursos
0 100%
B C D
Gerência de Recursos
0 100%
C D
Gerência de Recursos
0 100%
C D A
Gerência de Recursos
Activity:
onSaveInstanceState(Bundle state)
onRestoreInstanceState(Bundle
state)
onCreate(Bundle state)
Gerência de Recursos
Fragment:
onSaveInstanceState(Bundle state)
setRetainInstance(boolean retain)
onCreate(Bundle state)
onCreateView(LayoutInflater inflater
,ViewGroup group, Bundle state)
onActivityCreated(Bundle state)
Gerência de Recursos
onCreateonStartonResume
Gerência de Recursos
onCreateonStartonResume
Gerência de Recursos
onCreateonStartonResume
onSaveInstanceStateonPauseonStoponDestroy
Gerência de Recursos
onCreateonStartonResume
onCreateonStartonResume
onSaveInstanceStateonPauseonStoponDestroy
Gerência de Recursos
onCreateonStartonResume
onCreateonStartonResume
onSaveInstanceStateonPauseonStoponDestroy State
Thread de UIpaint
onClickpaintpaint
onReceiveonItemSelected
paintonPauseonStoppaint
onDestroy
Thread de UIpaint
onClickpaintpaint
onReceiveonItemSelected
paintonPauseonStoppaint
onDestroy
ANR
Thread de UIpaint
onClickpaintpaint
onReceiveonItemSelected
paintonPauseonStoppaint
onDestroy
Thread de UIpaint
onClickpaintpaint
onReceiveonItemSelected
paintonPauseonStoppaint
onDestroy
Worker Thread
Thread de UIpaint
onClickpaintpaint
onReceiveonItemSelected
paintonPauseonStoppaint
onDestroy
Worker Thread
Threading
AsyncTask
Threadingpublic class Activity { public void onCreate(Bundle
savedInstance) { ... DownloadTask task = new
DownloadTask(); task.execute(); ... }}
Threading
Loader
ThreadingAsyncQueryHandler
IntentService
Handler
java.lang.Thread #sqn
Sincronização
SyncAdapter
GCM
Polling
Memory Leaks
http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-
android/
Romain Guy, 2008
Memory Leak - Problemapublic class Util { private Context context; private static Util singleton;
public static Util get(Context context) { if (singleton == null) { this.singleton = new Util(context); } return singleton;
}
public String getHello() { return context.getString(R.string.hello);
}}
Memory Leak - Problema
public class MyActivity extends Activity { public void onCreate(Bundle
savedInstance) { ... Util util = Util.get(this); String hello =
util.getHello(); ...}
Memory Leak - Solução 1
public static Util get(Context context) { if (singleton == null) { Context ctx = context .getApplicationContext(); this.singleton = new Util(ctx);}return singleton;
}
Memory Leak - Solução 2
public class Util {
public static String getHello(Context ctx) { return ctx.getString( R.string.hello);}
}
Memory Leak - Solução 2public class MyActivity { public void onCreate(Bundle
savedInstance) { ... String hello = Util.getHello(this); ...}
}
Perguntas?
Muito Obrigado!
Ivan de Aguirre@IvAguirre