21
Intent & Share Anuchit Chalothorn [email protected] 7 Anuchit Chalothorn [email protected] Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Android App Development 07 : Intent & Share

Embed Size (px)

Citation preview

Page 1: Android App Development 07 : Intent & Share

Intent & ShareAnuchit [email protected]

7Anuchit [email protected]

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Page 2: Android App Development 07 : Intent & Share

Intent

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system.

Ref: http://developer.android.com/reference/android/content/Intent.html

Page 3: Android App Development 07 : Intent & Share

Intent to another activity

Intent to the Android system to starts another activity

Intent intent = new Intent(this,NewActivity.class);startActivity(intent);

Page 4: Android App Development 07 : Intent & Share

Intent with data

An Intent can contain data. This data can be used by the receiving component. As data it may send the URL to the browser component which this browser should open and display.

String url="http://google.com";Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse(url));startActivity(intent);

Page 5: Android App Development 07 : Intent & Share

Explicit Intents

Explicit intents explicitly defines the component which 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);

Page 6: Android App Development 07 : Intent & Share

Implicit Intents

Implicit intents specify the action which should be performed and optionally data which provides data for the action.

Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse("http://google.com"));startActivity(intent);

Page 7: Android App Development 07 : Intent & Share

Using the share intent

Lots of Android applications allow you to share some data with other people, e.g. the Facebook, G+, Gmail and Twitter application. You can send data to one of this components.

Page 8: Android App Development 07 : Intent & Share

Share Intent Example

You 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);

Page 9: Android App Development 07 : Intent & Share

Receive Intent data

The component which receives the Intent can use the getIntent().getExtras() method call to get the extra data.

Bundle extras = getIntent().getExtras();if (extras != null) {

String value1 = extras.getString(Intent.EXTRA_TEXT);}

Page 10: Android App Development 07 : Intent & Share

Intent Filters

If an Intents is send to the Android system, it will determine suitable applications for this Intents. If several components have been registered for this type of Intents, Android offers the user the choice to open one of them.

Page 11: Android App Development 07 : Intent & Share

Intent Filters

An Intent Filter declares the capabilities of a component. It specifies what an activity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. IntentFilters are typically defined via the AndroidManifest.xml file

Page 12: Android App Development 07 : Intent & Share

Register your Activity as Browser

Register an Activity for the Intent which is triggered when someone wants to open a webpage

<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>

Page 13: Android App Development 07 : Intent & Share

Register your Activity for the Share Intent

Register an Activity for the ACTION_SEND Intent 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>

Page 14: Android App Development 07 : Intent & Share

Workshop: Explicit Intents

Create 2 Activities, first activity has a edit text and button after push the button it 'll sent data in edit text to the second activity.

Page 16: Android App Development 07 : Intent & Share

Workshop: Implicit Intents

Create an app to use a Spinner to select the Intent which should get trigger :

● Open Browser○ Intent.ACTION_VIEW○ Uri.parse("http://google.com");

● Call Someone○ Intent.ACTION_CALL○ Uri.parse("tel:0891234567");

● Dial○ Intent.ACTION_DIAL○ Uri.parse("tel:0891234567");

Page 17: Android App Development 07 : Intent & Share

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");

Page 19: Android App Development 07 : Intent & Share

Workshop: Share Intent

Create App with simple share, to share text in edit text to another application.

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

Page 21: Android App Development 07 : Intent & Share

End