Using Intents to launch 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>


This is the XML layout.Now to use the DIAL button we need the following java code...


btnDial.setOnClickListener(new OnClickListener() {
       
       public void onClick(View v) {
     
        
    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 automatically you could have used this intent

Intent.ACTION_CALL instead of Intent.ACTION_DIAL.


but this requires ading the following permission to the manifest file:

<uses-permission android:name="android.permission.CALL_PHONE">

Do it in Android manifest permission settings.






No comments:

Post a Comment