Opening phone dialer from an Android application
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 on a button click. Android system provides a standard action name called “ACTION_DIAL” to invoke the dialer. We will make use this action to invoke the the dialer from our application.
1. Create a new Android project namely “DialerDemo”
2. Select build target for the application
3. Enter application details
4. res/values/strings.xml
1
2
3
4
5
6
| <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, MainActivity!</ string > < string name = "app_name" >DialerDemo</ string > < string name = "dialer" >Open Dialer</ string > </ resources > |
5. res/layout/main.xml
12345678910111213<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
Button
android:id
=
"@+id/btn"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/dialer"
/>
</
LinearLayout
>
6. src/in/wptrafficanalyzer/dialerdemo/MainActivity.java
123456789101112131415161718192021222324252627282930313233343536package
in.wptrafficanalyzer.dialerdemo;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public
class
MainActivity
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Referring to the button object of the main.xml layout */
Button btn = (Button) findViewById(R.id.btn);
/** Defining a click event listener */
OnClickListener listener =
new
OnClickListener() {
@Override
public
void
onClick(View v) {
/** Creating an intent with the dialer's action name */
/** Since the intent is created with activity's action name, the intent is an implicit intent */
Intent intent =
new
Intent(
"android.intent.action.DIAL"
);
/** Starting the Dialer activity */
startActivity(intent);
}
};
/** Setting click event listener for the buttonS */
btn.setOnClickListener(listener);
}
}
- On clicking the button "Open Dialer", the given below "Phone Dialer" will be opened"
No comments:
Post a Comment