105
Intro to Android for iOS developers Chiu-Ki Chan @chiuki

Intro to Android for iOS developers

Embed Size (px)

DESCRIPTION

Curious about Android, but never found the time to look into it? Come to this session for an accelerated introduction. We will look at the basic structure of an Android app, then deep dive into two aspects that differentiate Android from iOS: layout and intents. How does the layout system deal with the myriad of Android devices? How do intents facilitate deep integration among Android apps?

Citation preview

Page 1: Intro to Android for iOS developers

Intro to Androidfor iOS developers

Chiu-Ki Chan@chiuki

Page 2: Intro to Android for iOS developers

@chiuki@chiuki

Page 3: Intro to Android for iOS developers

@chiuki@chiuki

Hello WorldLayouts

Intents & Components

Page 4: Intro to Android for iOS developers

Hello World

Page 5: Intro to Android for iOS developers

@chiuki

Page 6: Intro to Android for iOS developers

@chiuki

ViewController

Page 7: Intro to Android for iOS developers

@chiuki

Page 8: Intro to Android for iOS developers

@chiuki

Page 9: Intro to Android for iOS developers

@chiuki

xib

Page 10: Intro to Android for iOS developers

@chiuki

Page 11: Intro to Android for iOS developers

@chiuki

Page 12: Intro to Android for iOS developers

@chiuki

Page 13: Intro to Android for iOS developers

@chiuki

Page 14: Intro to Android for iOS developers

@chiuki

IBOutlet

Page 15: Intro to Android for iOS developers

@chiuki

Page 16: Intro to Android for iOS developers

@chiuki@chiuki

Center<TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />

Page 17: Intro to Android for iOS developers

@chiuki

Page 18: Intro to Android for iOS developers

@chiuki@chiuki

Button<Button android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/add" />

Page 19: Intro to Android for iOS developers

@chiuki

LinearLayout

Page 20: Intro to Android for iOS developers

@chiuki

Page 21: Intro to Android for iOS developers

@chiuki@chiuki

LinearLayout<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" android:onClick="add" /></LinearLayout>

Page 22: Intro to Android for iOS developers

@chiuki@chiuki

IBAction<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" android:onClick="add" /></LinearLayout>

Page 23: Intro to Android for iOS developers

@chiuki@chiuki

IBActionpublic class MainActivity extends Activity { private TextView countView; private int count = 0;

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countView = (TextView) findViewById(R.id.count); updateCount(); }

public void add(View v) { count += 1; updateCount(); }

private void updateCount() { countView.setText(String.valueOf(count)); }}

android:onClick="add"

Page 24: Intro to Android for iOS developers

@chiuki@chiuki

Update viewpublic class MainActivity extends Activity { private TextView countView; private int count = 0;

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countView = (TextView) findViewById(R.id.count); updateCount(); }

public void add(View v) { count += 1; updateCount(); }

private void updateCount() { countView.setText(String.valueOf(count)); }}

Page 25: Intro to Android for iOS developers

@chiuki

Page 26: Intro to Android for iOS developers

@chiuki

Page 27: Intro to Android for iOS developers

@chiuki

RelativeLayout

Page 28: Intro to Android for iOS developers

@chiuki

Page 29: Intro to Android for iOS developers

@chiuki

Page 30: Intro to Android for iOS developers

@chiuki

Page 31: Intro to Android for iOS developers

@chiuki

Page 32: Intro to Android for iOS developers

@chiuki

<RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /></RelativeLayout>

Page 33: Intro to Android for iOS developers

@chiuki

<RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /></RelativeLayout>

Page 34: Intro to Android for iOS developers

@chiuki

<RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /></RelativeLayout>

Page 35: Intro to Android for iOS developers

@chiuki

<RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /></RelativeLayout>

Page 36: Intro to Android for iOS developers

@chiuki

<RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /></RelativeLayout>

Page 37: Intro to Android for iOS developers

@chiuki@chiuki

Device Preview

Page 38: Intro to Android for iOS developers

@chiuki@chiuki

Bigger text on tablets

Page 39: Intro to Android for iOS developers

Resource Folders

Page 40: Intro to Android for iOS developers

@chiuki@chiuki

textSize<TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" />

res/layout/activity_main.xml

Page 41: Intro to Android for iOS developers

@chiuki@chiuki

res/values/dimens.xml<TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/counter_size" />

res/layout/activity_main.xml

<dimen name="counter_size" value="128sp" />

res/values/dimens.xml

Page 42: Intro to Android for iOS developers

@chiuki@chiuki

res/values-sw600dp/dimens.xml<TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/counter_size" />

res/layout/activity_main.xml

<dimen name="counter_size" value="128sp" />

<dimen name="counter_size" value="256sp" />

res/values/dimens.xml

res/values-sw600dp/dimens.xml

Page 43: Intro to Android for iOS developers

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCA, jaScreen size: small, large, sw600dp, h400dpScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

Page 44: Intro to Android for iOS developers

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCA, jaScreen size: small, large, sw600dp, h400dpScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/values-sw600dp/dimens.xml

Page 45: Intro to Android for iOS developers

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCA, jaScreen size: small, large, sw600dp, h400dpScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/drawable-hdpi/ic_launcher.png

Page 46: Intro to Android for iOS developers

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCA, jaScreen size: small, large, sw600dp, h400dpScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/drawable-hdpi/ic_launcher.png @2x

Page 47: Intro to Android for iOS developers

@chiuki@chiuki

Resource Folders

Type Variation

layoutvalues

drawablemenu

Language & Region: en, fr, fr-rCA, jaScreen size: small, large, sw600dp, h400dpScreen orientation: port, landScreen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpiPlatform version: v4, v11, v14UI mode: car, desk, television, appliance

http://developer.android.com/guide/topics/resources/providing-resources.html

res/layout-ja/name.xml

Page 48: Intro to Android for iOS developers

Activity Lifecycle

Page 49: Intro to Android for iOS developers

@chiuki@chiuki

onCreate()

Activity Lifecycle

Page 50: Intro to Android for iOS developers

@chiuki@chiuki

onCreate()

Activity Lifecycle

Page 51: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

Activity Lifecycle

Page 52: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()

Activity Lifecycle

Page 53: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

Activity Lifecycle

Page 54: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

Activity Lifecycle

Page 55: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()

Activity Lifecycle

Page 56: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()onStop()

Activity Lifecycle

Page 57: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()onStop()

viewWillDisappear:viewDidDisappear:

Activity Lifecycle

Page 58: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()onStop()

viewWillDisappear:viewDidDisappear:

onDestroy()

Activity Lifecycle

Page 59: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()onStop()

viewWillDisappear:viewDidDisappear:

onDestroy() viewDidUnload:

Activity Lifecycle

Page 60: Intro to Android for iOS developers

@chiuki@chiuki

onCreate() viewDidLoad:

onStart()onResume()

viewWillAppear:viewDidAppear:

onPause()onStop()

viewWillDisappear:viewDidDisappear:

onDestroy() viewDidUnload:

Activity Lifecycle

Page 61: Intro to Android for iOS developers

@chiuki@chiuki

Visibility

Page 62: Intro to Android for iOS developers

More Activities

Page 63: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (explicit)Intent intent = new Intent(this, NumberActivity.class);intent.putExtra("count", count);startActivity(intent);

Page 64: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (explicit)Intent intent = new Intent(this, NumberActivity.class);intent.putExtra("count", count);startActivity(intent);

Page 65: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (explicit)Intent intent = new Intent(this, NumberActivity.class);intent.putExtra("count", count);startActivity(intent);

Page 66: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (implicit)Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 67: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (implicit)Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 68: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (implicit)Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 69: Intro to Android for iOS developers

@chiuki@chiuki

startActivity (implicit)Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 70: Intro to Android for iOS developers

@chiuki@chiuki

Register your Activity<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Info.plist

Page 71: Intro to Android for iOS developers

@chiuki@chiuki

IntentFilter: Action<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 72: Intro to Android for iOS developers

@chiuki@chiuki

IntentFilter: Data<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 73: Intro to Android for iOS developers

@chiuki@chiuki

IntentFilter: Category<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 74: Intro to Android for iOS developers

@chiuki@chiuki

IntentFilter: Category<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://twitter.com/chiuki"));startActivity(intent);

Page 75: Intro to Android for iOS developers

@chiuki@chiuki

Intent Data<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />  </intent-filter></activity>

Uri uri = getIntent().getData();

AndroidManifest.xml

YourActivity.java

Page 76: Intro to Android for iOS developers

@chiuki@chiuki

startActivityForResultprivate static final int REQUEST_CODE_CAMERA = 1;Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, REQUEST_CODE_CAMREA);

protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); }}

Page 77: Intro to Android for iOS developers

@chiuki@chiuki

startActivityForResultprivate static final int REQUEST_CODE_CAMERA = 1;Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, REQUEST_CODE_CAMERA);

protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); }}

Page 78: Intro to Android for iOS developers

@chiuki@chiuki

Providing ResultIntent data = new Intent();data.putExtra("data", bitmap);setResult(RESULT_OK, data);finish();

protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); }}

Page 79: Intro to Android for iOS developers

@chiuki@chiuki

Intent DataIntent data = new Intent();data.putExtra("data", bitmap);setResult(RESULT_OK, data);finish();

protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); }}

Page 80: Intro to Android for iOS developers

@chiuki@chiuki

Result CodeIntent data = new Intent();data.putExtra("data", bitmap);setResult(RESULT_OK, data);finish();

protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); }}

Page 81: Intro to Android for iOS developers

@chiuki@chiuki

Share intent

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_SUBJECT, subject);intent.putExtra(Intent.EXTRA_TEXT, message);

intent.setType("image/*");Uri uri = Uri.fromFile(new File(path));intent.putExtra(Intent.EXTRA_STREAM, uri);

Page 82: Intro to Android for iOS developers

@chiuki@chiuki

IntentFilter: Type<activity android:name=".YourActivity"> <intent-filter>   <action android:name="android.intent.action.ACTION_SEND" />    <category android:name="android.intent.category.DEFAULT" />   <data android:mimeType="image/*" />  </intent-filter></activity>

AndroidManifest.xml

Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("image/*");

Page 83: Intro to Android for iOS developers

@chiuki@chiuki

Intent Data

String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);String message = intent.getStringExtra(Intent.EXTRA_TEXT);Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

YourActivity.java

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_SUBJECT, subject);intent.putExtra(Intent.EXTRA_TEXT, message);

intent.setType("image/*");Uri uri = Uri.fromFile(new File(path));intent.putExtra(Intent.EXTRA_STREAM, uri);

Page 84: Intro to Android for iOS developers

Beyond Activities

Page 85: Intro to Android for iOS developers

@chiuki@chiuki

Service BroadcastReceiver

ContentProvider Notification

Page 86: Intro to Android for iOS developers

@chiuki@chiuki

Service

Look ma, no UI!

Page 87: Intro to Android for iOS developers

@chiuki@chiuki

Service

Long-running

Page 88: Intro to Android for iOS developers

@chiuki@chiuki

BroadcastReceiver

Respond

Page 89: Intro to Android for iOS developers

@chiuki@chiuki

BroadcastReceiver

Respond

Battery low

Wifi connected

Incoming SMSApp installed

Screen off

Page 90: Intro to Android for iOS developers

@chiuki@chiuki

ContentProvider

Provide data to other apps

Page 91: Intro to Android for iOS developers

@chiuki@chiuki

ContentProvider

Provide data to other apps

Contacts

Images Music

Calendar

Page 92: Intro to Android for iOS developers

@chiuki@chiuki

ContentProvider

Internal data(unless you need SQLite interface)

Page 93: Intro to Android for iOS developers

@chiuki@chiuki

Notification

Notify

Page 94: Intro to Android for iOS developers

@chiuki@chiuki

Notify

Page 95: Intro to Android for iOS developers

@chiuki@chiuki

Notification

Communicate with background Service

Page 96: Intro to Android for iOS developers

@chiuki@chiuki

Background Service

Page 97: Intro to Android for iOS developers

Case study

Page 98: Intro to Android for iOS developers

@chiuki

Email Outbox

Page 99: Intro to Android for iOS developers

@chiuki

Page 100: Intro to Android for iOS developers

@chiuki@chiuki

Notification

Page 101: Intro to Android for iOS developers

@chiuki@chiuki

AlarmService

Page 102: Intro to Android for iOS developers

@chiuki@chiuki

Connectivity change

Page 103: Intro to Android for iOS developers

Summary

Page 104: Intro to Android for iOS developers

@chiuki@chiuki

Summary

Hello WorldLayoutsResource FoldersActivity LifecycleIntents & ComponentsCase Study

Page 105: Intro to Android for iOS developers

@chiuki@chiuki

Thank you!

http://eepurl.com/lR5uDhttp://blog.sqisland.comhttp://twitter.com/chiuki