Intents are used by an application to interact with the phone's hardware components or other applications, or to start a service or activity with a certain peice of data or to broadcast that an event has occurred.
Using Intents to launch phone activities:
we can use Intents to launch the phone's basic activities such as the phone dialer, the browser or search.
these intents are called implicit intents cause you don't specify the activity you want to launch, rather Android determines the proper activity to launch based on the required action. also when the launched activity finisheds its work, the original activity has no information that the launched activity has finished it's work
in this example we create an intent that performs a phone number dial action. we don not specify that we want the dialer activity to launch, rather we specify that we want to dial a number and Android launches the dialer activity to perform this action
consider this activity: consists of a TextView and a button to dial the number in the text view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtNumber"
android:inputType="phone"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dial"
android:id="@+id/btnDial"
/>
</LinearLayout>
When you press the button the phone dialer launches and then you can call the number.
this is done using the following code
btnDial.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent dialIntent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+(txtNumber.getText()).toString()));
startActivity(dialIntent);
}
});
notice that the dialer has been launched but the user has to press the call button to make a call.
if you want the phone to dial the number autom atically you could have used this intent
Intent.ACTION_CALLbut this requires ading the following permission to the manifest file:
<uses-permission android:name="android.permission.CALL_PHONE">
and that was how to launch the phone activities using intents.
for a list of available phone actions, check this link
No comments:
Post a Comment