Introduction to android

Click here to load reader

  • date post

    15-Jan-2015
  • Category

    Technology

  • view

    60
  • download

    0

Embed Size (px)

description

GDG Bucharest first meetup 2013

Transcript of Introduction to android

2. Activities and UI 3. Activities and UIIntents 4. Activities and UI IntentsServices 5. Activities and UI IntentsBroadcast ReceiversServices 6. Activities and UI 7. Activities and UIA screenApplication = activity 8. Activity Lifecycle 9. Activity Lifecycle Managed by ActivityManager 10. Activity Lifecycle $ Managed by ActivityManager Developer says what happens at each state 11. Activity LifecycleFirst time runD/MyActivity( 1146): onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146):onResume Open another activity, then Back buttonD/MyActivity( 1146):onClickAnotherActivityD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onRestartD/MyActivity( 1146):onStartD/MyActivity( 1146): onResume 12. Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActivity( 1146):onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146): onResume 13. Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActivity( 1146):onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146): onResumeHome ButtonD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStop 14. Activity Lifecyclepackage ro.gdgcluj.demoapp;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class MyActivity extends Activity { static final String TAG = MyActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Log.d(TAG, "onCreate"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } 15. @Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause");}@Overrideprotected void onRestart() {super.onRestart();Log.d(TAG, "onRestart");}@Overrideprotected void onStop() {super.onStop();Log.d(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy");}} 16. Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml...... >... 17. Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml...... >...For your main activity use Intent Filters ... > ... 18. Building Android UI XMLDeclare UI in XMLInflate XML in Java files 19. Building Android UI XMLProgrammaticallyDeclare UI in XML VS. Initialize new widgetsInflate XML in Java files Customize properties for each 20. Building Android UI XMLProgrammaticallyDeclare UI in XML VS. Initialize new widgetsInflate XML in Java files Customize properties for eachUse them both 21. Layouts and views hierarchy 22. Intents 23. IntentsUsed to start activities, start/stop services, or send broadcasts 24. Using IntentsstartActivity(Intent activity);startService(Intent service);stopService(Intent service);sendBroadcast(Intent intent); 25. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class)); 26. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));Implicit IntentsstartService(new Intent("example.intent.action.IntentService"));sendBroadcast(new Intent("example.intent.action.Receiver")); 27. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));Implicit IntentsstartService(new Intent("example.intent.action.IntentService"));sendBroadcast(new Intent("example.intent.action.Receiver"));AndroidManifest.xml 28. Intent FiltersActivity Action ServiceReceiver 29. Intent FiltersActivity Action ServiceReceiverAndroidManifeset.xml 30. Services 31. ServicesRun in backgroundDont have UIRun on the UI thread 32. ServicesRun in backgroundUI ActivityDont have UIstartService(); stopService();Run on the UI thread Service 33. Service LifecycleService starts and "runs" until itgets a request to stopTo offload work from main thread, use intentservice. Intent service uses worker thread, stops when done with work. 34. Service Examplepackage ro.gdgcluj.demoapp;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service {static final String TAG = MyService.class.getSimpleName();@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {Log.d(TAG, "onCreate");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand");return START_STICKY;}@Overridepublic void onDestroy() {Log.d(TAG, "onDestroy");}} 35. Declaring the Service Called via its class name Called via action 36. Broadcast Receivers 37. Broadcast Receivers Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o. 38. Broadcast Receivers Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o.Register for certain intents Get notified when intent happens 39. Broadcast Receiver Examplepackage ro.gdgcluj.demoapp;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver {static final String TAG = Receiver.class.getSimpleName();@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "onReceive action: "+intent.getAction() );}} 40. Registering the Broadcast ReceiverDeclaring it in AndroidManifest.xml 41. Registering the Broadcast ReceiverRegistering Programmatically @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Create the receiver receiver = new Receiver(); filter = new IntentFilter( ANY_INTENT_ACTION ); } protected void onResume() { super.onResume(); super.registerReceiver(receiver, filter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } 42. Thats all!Questions 43. THANK YOUEugeniu Arbuleac Andrei [email protected] @[email protected] [email protected]