=>Open an existing project (SmsProject in our case) or create a new one. Right click on app, then Select New->Activity->BlankActivity. In Activity Name text box, type PhoneCall and Click on Finish. A new Activity will be created.

=>Now, we want to set this PhoneCall Activity as the Default Activity of our app. Open the AndroidManifest.xml file. Notice the tags:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Select the above tags, cut them and paste them after the tag:
<activity
android:name=".PhoneCall"
android:label="@string/title_activity_phone_call" >
After this, the file should look like:

=>Add the permission to call a number by adding the tag:
<uses-permission android:name="android.permission.CALL_PHONE" />
=>Now click on activity_phone_call.xml file. Delete the "hello world" TextView and drag a Number text field from the pallete into the phone's screen.

=>Double Click on the Text Box and change the id to "num". Similarly, add a Button to the Activity and change its text to "Call".


=>In the onClick event of the Button, type "callNow". This function will be defined inside the "PhoneCall.java" file, which will be called when the user clicks on this button.

=>Now open the PhoneCall.java file and add the following function inside the PhoneCall class:
public void callNow(View view)
{
EditText e1 = (EditText) findViewById(R.id.num);
Intent callIntent = new Intent(Intent.ACTION_CALL); //implicit intent
callIntent.setData((Uri.parse("tel:" + e1.getText().toString())));
startActivity(callIntent);
}

That's it! You can see how it works by copying the generated apk file onto your android device. Here are some screenshots of the app running on the default emulator:




Feel free to comment, suggest, ask or share!
No comments:
Post a Comment