1. 7
Intent & Share
Anuchit Chalothorn
anoochit@gmail.com
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
2. 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
3. Intent to another activity
Intent to the Android system to starts another
activity
Intent intent = new Intent(this,NewActivity.class);
startActivity(intent);
4. 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);
5. 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);
6. 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);
7. 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.
8. 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);
9. 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);
}
10. 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.
11. 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
12. 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>
13. 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>
14. 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.
15.
16. 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");
17. 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");
18.
19. 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);