INTENTS
INTENTS Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture.
INTENTS Intents are objects of the android.content.intent type. Your code can send them to the Android system defining the components you are targeting. For example, via the startactivity() method you can define that the intent should be used to start an activity. An intent can contain data via a Bundle. This data can be used by the receiving component.
INTENTS Android intents are mainly used to: Start the service Launch an activity Display a web page Display a list of contacts Broadcast a message Dial a phone call etc.
INTENTS Android intents are used to: Open another Activity or Service from the current Activity Pass data between Activities and Services Delegate responsibility to another application. For example, you can use Intents to open the browser application to display a URL.
Types of Android Intents There are two types of intents in android. Implicit Intents Explicit Intents
Implicit Intents Implicit Intent doesn't specify the component. Intent provides information of available components provided by the system that is to be invoked. Intent intent=new Intent(Intent.ACTION_VIEW); intent.setdata(uri.parse("http://www.msrit.edu")); startactivity(intent);
Explicit Intents Explicit Intent specifies the component. Intent provides the external class to be invoked. Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startactivity(intent);
Implicit Intent Example In the activity_main.xml create two buttons <Button android:id="@+id/btn_makecalls" <Button android:id="@+id/btn_webbrowser" android:layout_width="fill_parent" android:layout_height="wrap_conte nt" android:text="web Browser" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="172dp" android:text="make Calls" />
Implicit Intent Example In MainActivity.java file add action listener for the first button. b1 = (Button) findviewbyid(r.id.btn_webbrowser); b1.setonclicklistener(new OnClickListener() { } }); public void onclick(view v){ Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startactivity(i);
Implicit Intent Example In MainActivity.java file add action listener for the second button. b2 = (Button) findviewbyid(r.id.btn_makecalls); b2.setonclicklistener(new OnClickListener() { } }); public void onclick(view v){ Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("tel:+651234567")); startactivity(i);
Implicit Intent Example In Android, intents usually come in pairs: action and data. The action describes what is to be performed, such as editing an item, viewing the content of an item, and so on. The data specifies what is affected, such as a person in the Contacts database. The data is specified as an Uri object. Some examples of action are as follows: ACTION_VIEW ACTION_DIAL
Implicit Intent Example Collectively, the action and data pair describes the operation to be performed. For example, to dial a phone number, you would use the pair ACTION_DIAL/tel:+651234567. To display a list of contacts stored in your phone, you use the pair ACTION_VIEW/content://contacts. To pick a contact from the list of contacts, you use the pair ACTION_PICK/content://contacts.
Implicit Intent Example Intent i = new Intent( android.intent.action.view, Uri.parse( http://www.amazon.com )); startactivity(i); OR Intent i = new Intent( android.intent.action.view ); i.setdata(uri.parse( http://www.amazon.com )); startactivity(i);
Explicit Intent Create a new Android Project. Right-click on the package name under the src folder and select New Class. Name it as SecondActivity.java Create one xml file in res/layout folder and name it as secondactivity.xml Populate the second activity using secondactivity.xml file Add a button in activity_main.xml and modify <Button android:layout_width= fill_parent android:layout_height= wrap_content android:text= Display second activity android:onclick= onclick />
Explicit Intent Modify MainActivity.java file: public void onclick(view view) { } startactivity(new Intent( com.example.testintent.secondactivity ));
Explicit Intent Modify AndroidManifest.xml file: <activity android:name="com.example.testintent.secondactivity android:label="second Activity" > <intent-filter> <action android:name="com.example.testintent.secondactivity" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity>
Explicit Intent
The End