Android App Development 07 : Intent & Share


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android App Development 07 : Intent & Share

  1. 1. 7 Intent & Share Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. IntentIntents are asynchronous messages whichallow Android components to requestfunctionality from other components of theAndroid system.Ref:
  3. 3. Intent to another activityIntent to the Android system to starts anotheractivityIntent intent = new Intent(this,NewActivity.class);startActivity(intent);
  4. 4. Intent with dataAn Intent can contain data. This data can beused by the receiving component. As data itmay send the URL to the browser componentwhich this browser should open and display.String url="";Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse(url));startActivity(intent);
  5. 5. Explicit IntentsExplicit intents explicitly defines the componentwhich should be called by the Android system,by using the Java class as identifier.Intent i = new Intent(this,NewActivity.class);i.putExtra("Value1", "This value one for ActivityTwo ");i.putExtra("Value2", "This value two ActivityTwo");startActivity(i);
  6. 6. Implicit IntentsImplicit intents specify the action which shouldbe performed and optionally data whichprovides data for the action.Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse(""));startActivity(intent);
  7. 7. Using the share intentLots of Android applications allow you to sharesome data with other people, e.g. theFacebook, G+, Gmail and Twitter application.You can send data to one of this components.
  8. 8. Share Intent ExampleYou can send data to one of this components.Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(android.content.Intent.EXTRA_TEXT,"News for you!");startActivity(intent);
  9. 9. Receive Intent dataThe component which receives the Intent canuse the getIntent().getExtras() method call toget the extra data.Bundle extras = getIntent().getExtras();if (extras != null) { String value1 = extras.getString(Intent.EXTRA_TEXT);}
  10. 10. Intent FiltersIf an Intents is send to the Android system, itwill determine suitable applications for thisIntents. If several components have beenregistered for this type of Intents, Android offersthe user the choice to open one of them.
  11. 11. Intent FiltersAn Intent Filter declares the capabilities of acomponent. It specifies what an activity orservice can do and what types of broadcasts aReceiver can handle. It allows thecorresponding component to receive Intents ofthe declared type. IntentFilters are typicallydefined via the AndroidManifest.xml file
  12. 12. Register your Activity as BrowserRegister an Activity for the Intent which istriggered when someone wants to open awebpage<activity android:name=".BrowserActivitiy" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http"/> </intent-filter></activity>
  13. 13. Register your Activity for the Share IntentRegister an Activity for the ACTION_SENDIntent for the text/plain mime type.<activity android:name=".ActivityTest" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter></activity>
  14. 14. Workshop: Explicit IntentsCreate 2 Activities, first activity has a edit textand button after push the button it ll sent datain edit text to the second activity.
  15. 15. Workshop: Implicit IntentsCreate an app to use a Spinner to select theIntent which should get trigger : ● Open Browser ○ Intent.ACTION_VIEW ○ Uri.parse(""); ● Call Someone ○ Intent.ACTION_CALL ○ Uri.parse("tel:0891234567"); ● Dial ○ Intent.ACTION_DIAL ○ Uri.parse("tel:0891234567");
  16. 16. Workshop: Implicit Intents ● Show Map ○ Intent.ACTION_VIEW ○ Uri.parse("geo:0,0?q=100.00,15.00"); ● Search on Map ○ Intent.ACTION_VIEW ○ Uri.parse("geo:0,0?q=Bluecup coffee");
  17. 17. Workshop: Share IntentCreate App with simple share, to share text inedit text to another application.Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_TEXT, shareText);startActivity(intent);
  18. 18. End