Intent to call a number

Description

An application to call a telephone number.

Example


package com.java2s.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
//from ww  w .j  av a 2s .c om
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout parentContainer = new LinearLayout(this);
        parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        parentContainer.setOrientation(LinearLayout.VERTICAL);

        Button button = new Button(this);
        parentContainer.addView(button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("tel:+651234567"));
                startActivity(i);
            }
        });

        setContentView(parentContainer);
    }
}

The code above generates the following result.

Intent to call a number

Note

You dial a specific number by passing in the telephone number in the data portion:


Intent i = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse ("tel:+651234567"));
startActivity(i);

The dialer will display the number to be called. The user must still press the dial button to dial the number.

If you want to directly call the number without user intervention, change the action as follows:


Intent i = new Intent(android.content.Intent.ACTION_CALL,Uri.parse ("tel:+651234567"));
startActivity(i);

If you want your application to directly call the specified number, you need to add the android.permission.CALL_PHONE permission to your application.

To display the dialer without specifying any number, simply omit the data portion, like this:


Intent i = new Intent(android.content.Intent.ACTION_DIAL);
startActivity(i);




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS