12

I am trying to use the Intent.Action class. I know how to use the ACTION_VIEW to display a URL but I wanted to use the Intent.ACTION_DIAL to call number when the application is launched. The documentation says you need to parse a URI into a string and then add it to the Intent I tried this:

Uri call = Uri.parse("7777777777");             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

This doesn't work I get an error message saying:

Unfortunately, Project has stopped. I tried to debug the code and it seems to point me to the intent line not sure what I doing wrong if I just do this it works and brings up the dialer.

//Uri call = Uri.parse("7777777777");               
Intent surf = new Intent(Intent.ACTION_DIAL);   
startActivity(surf);
Lukas Ruge
  • 2,204
  • 3
  • 30
  • 42
Reazur Rahman
  • 151
  • 1
  • 1
  • 12
  • possible duplicate of [Call intent in Android](http://stackoverflow.com/questions/10510395/call-intent-in-android) – amadib Mar 12 '15 at 18:35

9 Answers9

24

tel

String number = "23454568678";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);

Use Permission

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>   
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • 2
    no you should not use either `Intent.ACTION_CALL` nor `Manifest.permission.CALL_PHONE` but `Intent.ACTION_DIAL` see https://support.google.com/googleplay/android-developer/answer/9047303 – childno͡.de Oct 14 '19 at 13:46
20

To just open the dialer app (the user has to press the call button inside the dialer app; no additional permissions needed) use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

To open the dialer app and do the call automatically (needs android.permission.CALL_PHONE) use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_CALL, call); 
startActivity(surf);
Harry
  • 907
  • 10
  • 11
4

try this

String url="tel:777777777"
if (url.startsWith("tel:")) { 
  Intent intent = new Intent(Intent.ACTION_DIAL,
  Uri.parse(url)); 
  startActivity(intent);
}

add this to your AndroidManifest.xml file

<uses-permission android:name="android.permission.CALL_PHONE" />
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Pragadees
  • 326
  • 3
  • 8
4

Try this also

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phno);
startActivity(intent);

Android Manifest

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
Pragadees
  • 326
  • 3
  • 8
2

try this

String no = "536171839";
Intent callintent = new Intent(android.intent.action.CALL);
callintent.setData(Uri.parse("tel:" +no));
startActivity(callintent);

add this to your AndroidManifest.xml file

 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
fidazik
  • 423
  • 1
  • 5
  • 11
2

Try this :

String toCall = "tel:" + number.getText().toString();

startActivity(new Intent(Intent.ACTION_DIAL,

Uri.parse(toCall)));
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Delgado
  • 216
  • 3
  • 16
2

Another approach is to make an PendingIntent to be called later. This is specially util when you want to redirect the user directly to phone call from a notification Action.

String number = "551191111113";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
PendingIntent pendingIntentForCall = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);

You can use it in notification as follow:

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setTicker(tickerText)
                .setColor(Color.BLACK)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_directions_bus_white_48dp))
                .setSmallIcon(R.mipmap.ic_directions_bus_white_24dp)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .addAction(new NotificationCompat.Action(R.mipmap.ic_directions_bus_white_24dp,"Call to " + number,pendingIntentForCall));
Vinicius Lima
  • 1,601
  • 1
  • 12
  • 15
1

If u have added

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

Check the permission of call on the phone for your application.

Emma Wang
  • 11
  • 2
0

For ACTION_DIAL you just need to create Intent object with that action as a first argument and Uri object as a second argument built from phone number written as a string. After that just call startActivity() method and pass previously created intent object as an argument. For example:

private String phoneNumber = "123456";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button dial_number = findViewById(R.id.button);
    dial_number.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
            startActivity(intent);
        }
    });
}
Hadži Lazar Pešić
  • 1,031
  • 1
  • 13
  • 20