Intent & ShareAnuchit [email protected]
7Anuchit [email protected]
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
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
Intent to another activity
Intent to the Android system to starts another activity
Intent intent = new Intent(this,NewActivity.class);startActivity(intent);
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);
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);
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);
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.
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);
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);}
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.
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
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>
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>
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.
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");
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");
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);
End
Top Related