Make a Phone Call Programmatically

How to make a phone call programmatically?

You forgot to call startActivity. It should look like this:

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);

An intent by itself is simply an object that describes something. It doesn't do anything.

Don't forget to add the relevant permission to your manifest:


How to make a call programmatically in android

ACTION_DIAL

Added in API level 1

String ACTION_DIAL

Activity Action: Dial a number as specified by the data. This shows a UI with the number being dialed, allowing the user to explicitly initiate the call.

Input: If nothing, an empty dialer is started, else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.

Constant Value: android.intent.action.DIAL


ACTION_CALL

Added in API level 1

String ACTION_CALL

Activity Action: Perform a call to someone specified by the data.

Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.

Note:

  • There will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
  • This Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL.
  • If you app targets android M and above and declares as using the CALL_PHONE
    permission which is not granted, then attempting to use this action
    will result in a SecurityException.

Constant Value: android.intent.action.CALL


so basically

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) and then use:

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

How to make phone calls programmatically in Android?

The problem is that this line:

TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);

is executed on the activity, so the findViewById will always return the first item with that id, which is likely the first item in the list.

The best way to fix this would be to override the adapter and add a tag containing the phone number to the view. A quick way to fix this would be to tag along in the view hierarchy, like so:

public void callNumber(View view) {
if( view != null ) { // view is the button tapped
View parent = view.getParent(); // this should be the LinearLayout
if( parent instanceof LinearLayout ) {
TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
if( unItemVal != null ) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
}
}
}

This would find the parent for the button that was clicked, and then find the text-view containing the number within that ViewGroup.

How to make the call directly programmatically

You can do this simply. It make the call directly.

 Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number")); 
startActivity(callIntent);

and add this permission in AndroidManifest.xml


Android dial a phone number programmatically

Use the below code:

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);

Make a phone call programmatically

Probably the mymobileNO.titleLabel.text value doesn't include the scheme //

Your code should look like this:

ObjectiveC

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

Swift

if let url = URL(string: "tel://\(mymobileNO.titleLabel.text))") {
UIApplication.shared.open(url)
}

Is it possible to programmatically make a call on background?

Yes it's possible for Android I think. You can use the AlarmManager or an Handler for the schedule problem. To decide which you have to take. Here an exceprt from the Android documentation:

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

For starting a call in Android you can take a look here.



Related Topics



Leave a reply



Submit