Transcript
Page 1: Intents: Talking to your neighbors

GDG Bootcamp 2013

IntentsTalking to your neighbors

+Christian Ketterer / cketti

Page 2: Intents: Talking to your neighbors

GDG Bootcamp 2013 2

What are Intents again?

● „An intent is an abstract description of an operation to be performed.“

→ stores data that is interpreted by others● It can be used to...

– start an activity → startActivity()

– send a broadcast → sendBroadcast()

– start a service → startService() & bindService()

Page 3: Intents: Talking to your neighbors

GDG Bootcamp 2013 3

Structure of an Intent

● Action: String● Data: Uri● Type: String● Component● ...

● Extras:– all primitive Java types

– CharSequence

– Serializable

– Parcelable

– Bundle– Arrays/lists of the

above types

Page 4: Intents: Talking to your neighbors

GDG Bootcamp 2013 4

Explicit intents

● Specify the component name

Intent intent = new Intent(context, MyActivity.class);

startActivity(intent);

→ Boring!

Page 5: Intents: Talking to your neighbors

GDG Bootcamp 2013 5

Using implicit intents

● Describe what you want to do

→ That's why it's called intent!● Let Android figure out which app to use to perform

the action, i.e. don't specify a component.

Uri webpage = Uri.parse("http://c-base.org");

Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

Page 6: Intents: Talking to your neighbors

GDG Bootcamp 2013 6

Generic actions*

● ACTION_VIEW● ACTION_PICK● ACTION_GET_CONTENT● ACTION_SEND● ACTION_SENDTO

● ACTION_INSERT● ACTION_DELETE● ACTION_EDIT● ACTION_SEARCH● ACTION_DIAL● and many more

* Specified in android.content.Intent

Page 7: Intents: Talking to your neighbors

GDG Bootcamp 2013 7

Special actions

● MediaStore.ACTION_IMAGE_CAPTURE● "com.google.zxing.client.android.SCAN"● "org.openintents.action.PICK_DIRECTORY"● "org.androidprinting.intent.action.PRINT"

● Define your own● Find more on OpenIntents.org

Page 8: Intents: Talking to your neighbors

GDG Bootcamp 2013 8

Special actions

● MediaStore.ACTION_IMAGE_CAPTURE● "com.google.zxing.client.android.SCAN"● "org.openintents.action.PICK_DIRECTORY"● "org.androidprinting.intent.action.PRINT"

● Define your own● Find more on OpenIntents.org

Page 9: Intents: Talking to your neighbors

GDG Bootcamp 2013 9

Example: Create a contact

Intent intent = new Intent(Intent.ACTION_INSERT);

intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

intent.putExtra(ContactsContract.Intents.Insert.NAME,"John Doe");

intent.putExtra(ContactsContract.Intents.Insert.EMAIL,"[email protected]");

startActivity(intent);

Page 10: Intents: Talking to your neighbors

GDG Bootcamp 2013 10

Example: Show an image

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(imageUri, "image/jpeg");

startActivity(intent);

Page 11: Intents: Talking to your neighbors

GDG Bootcamp 2013 11

Sharing

● ACTION_SEND● Probably the most commonly used implicit

intent● You can share...

– Text → EXTRA_TEXT– Arbitrary data → EXTRA_STREAM

Page 12: Intents: Talking to your neighbors

GDG Bootcamp 2013 12

Example: Sharing text

Page 13: Intents: Talking to your neighbors

GDG Bootcamp 2013 13

Example: Sharing text

Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_TEXT, "Some text");

Intent c = Intent.createChooser(intent, "Share via");startActivity(c);

● Use Intent.createChooser() to always display the chooser dialog

Page 14: Intents: Talking to your neighbors

GDG Bootcamp 2013 14

Get results

● Use startActivityForResult() instead of startActivity()

● The method onActivityResult() in your activity is called with an intent containing the result

Page 15: Intents: Talking to your neighbors

GDG Bootcamp 2013 15

Example: Take a picture

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, IMAGE_REQUEST_CODE);

protected void onActivityResult(int requestCode,int resultCode, Intent data) {

if (resultCode == RESULT_OK &&

requestCode == IMAGE_REQUEST_CODE) {Bundle extras = data.getExtras();Bitmap bitmap = (Bitmap) extras.get("data");mImageView.setImageBitmap(bitmap);

}}

Page 16: Intents: Talking to your neighbors

GDG Bootcamp 2013 16

Allowing other apps to start your activity

● Add an intent filter to your manifest● The intent is delivered to your activity just like

an explicit intent

<activity android:name="ShareActivity"><intent-filter>

<action android:name="android.intent.action.SEND"/><category android:name="android.intent.category.DEFAULT"/><data android:mimeType="text/plain"/><data android:mimeType="image/*"/>

</intent-filter></activity>

Page 17: Intents: Talking to your neighbors

GDG Bootcamp 2013 17

Returning a result

● Call setResult() before finishing your activity

Intent result = new Intent("com.example.RESULT_ACTION", resultUri);

setResult(RESULT_OK, result);finish();

Page 18: Intents: Talking to your neighbors

GDG Bootcamp 2013 18

Broadcasts

● System-wide broadcast announcements via Intents

● E.g. system broadcasts– ACTION_BOOT_COMPLETED– ACTION_TIMEZONE_CHANGED– ACTION_POWER_CONNECTED– ACTION_POWER_DISCONNECTED– ACTION_SHUTDOWN

Page 19: Intents: Talking to your neighbors

GDG Bootcamp 2013 19

Receiving broadcasts

<receiver android:name="Receiver"><intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter>

</receiver>

public void onReceive(Context context, Intent intent) {if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

// Do something}

}

AndroidManifest.xml

Receiver.java

Page 20: Intents: Talking to your neighbors

GDG Bootcamp 2013 20

Sending broadcasts

Intent intent = new Intent("com.perseum.bigRedButtonPressed");sendBroadcast(intent);

Page 21: Intents: Talking to your neighbors

GDG Bootcamp 2013 21

Services

● Can be used for awesomeness● The topic requires a talk of its own

– Intents don't play much of an important role anyway

Page 22: Intents: Talking to your neighbors

GDG Bootcamp 2013 22

TL;DL

● Use intents to integrate functionality of other apps

● Publish the documentation of your intents to give other developers a chance to integrate with your app

Page 23: Intents: Talking to your neighbors

GDG Bootcamp 2013 23

Further reading

● http://developer.android.com/training/basics/intents/index.html

● http://developer.android.com/training/sharing/index.html

● http://developer.android.com/guide/components/intents-filters.html

● http://www.slideshare.net/CodeAndroid/android-intent-intent-filter-broadcast-receivers


Top Related