Intents: Talking to your neighbors

Download Intents: Talking to your neighbors

Post on 23-Jun-2015

98 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li> 1. GDG Bootcamp 2013IntentsTalking to your neighbors+Christian Ketterer / cketti</li></ul> <p> 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() &amp; bindService()GDG Bootcamp 20132 3. Structure of an Intent Action: String Extras: Data: Uri all primitive Java types CharSequence Type: String Serializable Component Parcelable ... Bundle Arrays/lists of the above typesGDG Bootcamp 2013 3 4. Explicit intents Specify the component name Intent intent = new Intent(context, MyActivity.class); startActivity(intent); Boring!GDG Bootcamp 2013 4 5. Using implicit intents Describe what you want to do Thats why its called intent! Let Android figure out which app to use to perform the action, i.e. dont specify a component. Uri webpage = Uri.parse("http://c-base.org"); Intent intent = new Intent(Intent.ACTION_VIEW, webpage);GDG Bootcamp 2013 5 6. Generic actions* ACTION_VIEW ACTION_INSERT ACTION_PICK ACTION_DELETE ACTION_GET_CONTENT ACTION_EDIT ACTION_SEND ACTION_SEARCH ACTION_SENDTO ACTION_DIAL and many more* Specified in android.content.IntentGDG Bootcamp 2013 6 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.orgGDG Bootcamp 20137 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.orgGDG Bootcamp 20138 9. Example: Create a contactIntent 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,"doe@example.com");startActivity(intent);GDG Bootcamp 20139 10. Example: Show an image Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(imageUri, "image/jpeg"); startActivity(intent);GDG Bootcamp 201310 11. Sharing ACTION_SEND Probably the most commonly used implicit intent You can share... Text EXTRA_TEXT Arbitrary data EXTRA_STREAMGDG Bootcamp 2013 11 12. Example: Sharing textGDG Bootcamp 201312 13. Example: Sharing textIntent 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 dialogGDG Bootcamp 2013 13 14. Get results Use startActivityForResult() instead of startActivity() The method onActivityResult() in your activity is called with an intent containing the resultGDG Bootcamp 2013 14 15. Example: Take a pictureIntent 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 &amp;&amp; requestCode == IMAGE_REQUEST_CODE) {Bundle extras = data.getExtras();Bitmap bitmap = (Bitmap) extras.get("data");mImageView.setImageBitmap(bitmap); }}GDG Bootcamp 201315 16. Allowing other apps to startyour activity Add an intent filter to your manifest The intent is delivered to your activity just like an explicit intentGDG Bootcamp 201316 17. Returning a result Call setResult() before finishing your activityIntent result = new Intent("com.example.RESULT_ACTION", resultUri);setResult(RESULT_OK, result);finish();GDG Bootcamp 2013 17 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_SHUTDOWNGDG Bootcamp 201318 19. Receiving broadcasts AndroidManifest.xml Receiver.javapublic void onReceive(Context context, Intent intent) {if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {// Do something}}GDG Bootcamp 2013 19 20. Sending broadcasts Intent intent = new Intent("com.perseum.bigRedButtonPressed"); sendBroadcast(intent);GDG Bootcamp 2013 20 21. Services Can be used for awesomeness The topic requires a talk of its own Intents dont play much of an important roleanywayGDG Bootcamp 201321 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 appGDG Bootcamp 201322 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-receiversGDG Bootcamp 2013 23</p>