Phone dialer is an activity available with Android operating system to call a number. In this article, we will create an application which invokes the dialer Or make call on a button click
To make a phone call from an Android application, all you have to do is create a new Intent, either an Intent.ACTION_DIAL (to start the call) or Intent.ACTION_CALL (to place the call).
package in.blogspot.khurramitdeveloper;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button call = (Button) findViewById(R.id.btnCall);
Button Dialer = (Button) findViewById(R.id.btnDialer);
num = (TextView) findViewById(R.id.eTNumber);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = num.getText().toString();
Intent dialerIntent = new Intent();
dialerIntent.setAction(Intent.ACTION_CALL);
dialerIntent.setData(Uri.parse("tel:" + number));
startActivity(dialerIntent);
}
});
Dialer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent dialerIntent = new Intent();
dialerIntent.setAction(Intent.ACTION_DIAL);
startActivity(dialerIntent);
}
});
}
}
3) AndroidMenifest.xml
By default we don’t have privilege to invoke call activity, so explicitly we must add the permission in manifest.xml file.
<uses-permission android:name="android.permission.CALL_PHONE"/>
Add <uses-permission android:name=”android.permission.CALL_PHONE” /> under <uses-sdk />tag in manifest.xml which is present right under the project folder. Don’t forget to add permission!.
Everything is done, we are gonna test the code.
you can download complete working source code example here
http://www.mediafire.com/?2a3c39vfafa92nh
To make a phone call from an Android application, all you have to do is create a new Intent, either an Intent.ACTION_DIAL (to start the call) or Intent.ACTION_CALL (to place the call).
1) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgimage"
android:orientation="vertical" >
<EditText
android:id="@+id/eTNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Call" />
<Button
android:id="@+id/btnDialer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnCall"
android:text="Dialer" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgimage"
android:orientation="vertical" >
<EditText
android:id="@+id/eTNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Call" />
<Button
android:id="@+id/btnDialer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnCall"
android:text="Dialer" />
</LinearLayout>
2)MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button call = (Button) findViewById(R.id.btnCall);
Button Dialer = (Button) findViewById(R.id.btnDialer);
num = (TextView) findViewById(R.id.eTNumber);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = num.getText().toString();
Intent dialerIntent = new Intent();
dialerIntent.setAction(Intent.ACTION_CALL);
dialerIntent.setData(Uri.parse("tel:" + number));
startActivity(dialerIntent);
}
});
Dialer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent dialerIntent = new Intent();
dialerIntent.setAction(Intent.ACTION_DIAL);
startActivity(dialerIntent);
}
});
}
}
3) AndroidMenifest.xml
By default we don’t have privilege to invoke call activity, so explicitly we must add the permission in manifest.xml file.
<uses-permission android:name="android.permission.CALL_PHONE"/>
Add <uses-permission android:name=”android.permission.CALL_PHONE” /> under <uses-sdk />tag in manifest.xml which is present right under the project folder. Don’t forget to add permission!.
Everything is done, we are gonna test the code.
you can download complete working source code example here
http://www.mediafire.com/?2a3c39vfafa92nh
No comments:
Post a Comment