Download - Introduction to android
![Page 1: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/1.jpg)
Introduction to Android Development
(Part 1)
![Page 3: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/3.jpg)
Activities and UI
![Page 4: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/4.jpg)
Activities and UI
Intents
![Page 5: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/5.jpg)
Activities and UI
Intents
Services
![Page 6: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/6.jpg)
Activities and UI
Intents
Services
Broadcast Receivers
![Page 7: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/7.jpg)
Activities and UIActivities and UI
![Page 8: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/8.jpg)
Activities and UI
A screen
Application = Σ activity
![Page 9: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/9.jpg)
Activity Lifecycle
![Page 10: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/10.jpg)
Managed by ActivityManager
Activity Lifecycle
![Page 11: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/11.jpg)
Developer says what happens at each state
$
¢
Activity Lifecycle
Managed by ActivityManager
![Page 12: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/12.jpg)
D/MyActivity( 1146): onCreateD/MyActivity( 1146): onStartD/MyActivity( 1146): onResume
First time run
D/MyActivity( 1146): onClickAnotherActivityD/MyActivity( 1146): onPauseD/MyActivity( 1146): onStopD/MyActivity( 1146): onRestartD/MyActivity( 1146): onStartD/MyActivity( 1146): onResume
Open another activity, then Back button
Activity Lifecycle
![Page 13: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/13.jpg)
D/MyActivity( 1146): onPauseD/MyActivity( 1146): onStopD/MyActivity( 1146): onDestroyD/MyActivity( 1146): onCreateD/MyActivity( 1146): onStartD/MyActivity( 1146): onResume
Rotate screen
Activity Lifecycle
![Page 14: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/14.jpg)
D/MyActivity( 1146): onPauseD/MyActivity( 1146): onStop
Home Button
D/MyActivity( 1146): onPauseD/MyActivity( 1146): onStopD/MyActivity( 1146): onDestroyD/MyActivity( 1146): onCreateD/MyActivity( 1146): onStartD/MyActivity( 1146): onResume
Rotate screen
Activity Lifecycle
![Page 15: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/15.jpg)
package 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"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); }
@Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); }
@Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); }
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); }
}
Activity Lifecycle
![Page 16: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/16.jpg)
package 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"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); }
@Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); }
@Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); }
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); }
}
![Page 17: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/17.jpg)
Declaring the Activity
<manifest ... > <application ... > <activity android:name=".MyActivity" /> ... </application ... > ...</manifest >
Let your application know about your Activity into the AndroidManifest.xml
![Page 18: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/18.jpg)
For your main activity use Intent Filters
<manifest ... > <application ... > <activity android:name=".MyActivity" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> <intent-filter /> <activity /> </application ... > ...</manifest >
<manifest ... > <application ... > <activity android:name=".MyActivity" /> ... </application ... > ...</manifest >
Let your application know about your Activity into the AndroidManifest.xml
Declaring the Activity
![Page 19: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/19.jpg)
Building Android UI
XML
Declare UI in XML
Inflate XML in Java files
![Page 20: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/20.jpg)
Building Android UI
Programmatically
Initialize new widgets
Customize properties for each
VS.
XML
Declare UI in XML
Inflate XML in Java files
![Page 21: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/21.jpg)
Use them both
Programmatically
Initialize new widgets
Customize properties for each
VS.
XML
Declare UI in XML
Inflate XML in Java files
Building Android UI
![Page 22: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/22.jpg)
Layouts and views hierarchy
![Page 23: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/23.jpg)
IntentsIntents
![Page 24: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/24.jpg)
Used to start activities, start/stop services, or send broadcasts
Intents
![Page 25: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/25.jpg)
startActivity(Intent activity);
startService(Intent service);
stopService(Intent service);
sendBroadcast(Intent intent);
Using Intents
![Page 26: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/26.jpg)
startActivity(new Intent(this, TargetActivity.class));
startService(new Intent(this, TargetService.class));
Explicit Intents
![Page 27: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/27.jpg)
startService(new Intent("example.intent.action.IntentService"));
sendBroadcast(new Intent("example.intent.action.Receiver"));
Implicit Intents
startActivity(new Intent(this, TargetActivity.class));
startService(new Intent(this, TargetService.class));
Explicit Intents
![Page 28: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/28.jpg)
<service android:name=".IntentService"> <intent-filter> <action android:name="example.intent.action.IntentService" /> </intent-filter></service>
<receiver android:name=".Receiver"> <intent-filter> <action android:name="example.intent.action.Receiver" /> </intent-filter></receiver>
startService(new Intent("example.intent.action.IntentService"));
sendBroadcast(new Intent("example.intent.action.Receiver"));
Implicit Intents
startActivity(new Intent(this, TargetActivity.class));
startService(new Intent(this, TargetService.class));
Explicit Intents
AndroidManifest.xml
![Page 29: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/29.jpg)
Intent FiltersActivity
Service
Receiver
Action
![Page 30: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/30.jpg)
<intent-filter> <action android:name="any.action.you.want" /></intent-filter>
Intent Filters
Action
Activity
Service
Receiver
AndroidManifeset.xml
![Page 31: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/31.jpg)
ServicesServices
![Page 32: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/32.jpg)
Run in background
Don’t have UI
Run on the UI thread
Services
![Page 33: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/33.jpg)
UI ActivityUI Activity
ServiceService
startService(); stopService();
Services
Run in background
Don’t have UI
Run on the UI thread
![Page 34: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/34.jpg)
Service starts and "runs" until it gets a request to stop
To offload work from main thread, use intent service.
Intent service uses worker thread, stops when done with work.
Service Lifecycle
![Page 35: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/35.jpg)
package 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();
@Override public IBinder onBind(Intent arg0) { return null; }
@Override public void onCreate() { Log.d(TAG, "onCreate"); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return START_STICKY; }
@Override public void onDestroy() { Log.d(TAG, "onDestroy"); }
}
Service Example
![Page 36: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/36.jpg)
<service android:name=".ServiceDemo"></service>
Called via its class name
<service android:name=".IntentService"> <intent-filter> <action android:name="example.intent.action.IntentService" /> </intent-filter></service>
Declaring the Service
Called via action
![Page 37: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/37.jpg)
Broadcast ReceiversBroadcast Receivers
![Page 38: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/38.jpg)
Intent based publish-subscribe mechanism
Listening system events: incoming calls, SMS messages a.o.
Broadcast Receivers
![Page 39: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/39.jpg)
Register for certain intents
Get notified when intent happens
Intent based publish-subscribe mechanism
Listening system events: incoming calls, SMS messages a.o.
Broadcast Receivers
![Page 40: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/40.jpg)
package 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();
@Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive action: "+intent.getAction() ); }
}
Broadcast Receiver Example
![Page 41: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/41.jpg)
<receiver android:name=".ReceiverDemo"> <intent-filter> <action android:name="example.intent.action.Receiver" /> </intent-filter></receiver>
Declaring it in AndroidManifest.xml
Registering the Broadcast Receiver
![Page 42: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/42.jpg)
@Overrideprotected 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);}
@Overrideprotected void onPause() { super.onPause(); unregisterReceiver(receiver);}
Registering the Broadcast Receiver
Registering Programmatically
![Page 43: Introduction to android](https://reader033.vdocuments.pub/reader033/viewer/2022061218/54b6ce6c4a7959d84d8b45b9/html5/thumbnails/43.jpg)
Questions
That’s all!