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 th
If u have added
<uses-permission android:name="android.permission.CALL_PHONE" />
Check the permission of call on the phone for your application.
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>
Try this :
String toCall = "tel:" + number.getText().toString();
startActivity(new Intent(Intent.ACTION_DIAL,
Uri.parse(toCall)));
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));
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" />
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);
}
});
}