12

I seems to be lacking knowledge about handling such intents, however couldnt find answer for a while.

I have an activity with one fragment. The fragment executes this code in purpose of calling a contact:

private void onCall() {
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(contact.getBusinessPhone()));
    startActivity(intent);
}

Also included permission

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

The output is No Activity found to handle Intent and the app crashes.

Here is manifest implementation of the activity that holds fragment:

<activity android:name="activities.ContactActivity">            
    <intent-filter>
        <action android:name="android.intent.action.DIAL" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

What am I doing wrong? Do I need some special activity declared in manifest for that?

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157

2 Answers2

69

You don't need to declare dial intent-filter in manifest and don't need any permissions to ACTION_DIAL. Look for my implementation

private void startDialActivity(String phone){
    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:"+phone));
    startActivity(intent);
}

also is good to check is telephony supported on device

private boolean isTelephonyEnabled(){
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    return telephonyManager != null && telephonyManager.getSimState()==TelephonyManager.SIM_STATE_READY;
}
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
Viacheslav
  • 5,443
  • 1
  • 29
  • 36
  • 1
    Would give +2 if I could. 1 for an answer and 1 for pointing the isTelephonyEnabled problem, which is important if the app is also tablet targeted. thanks! – Jacek Kwiecień Apr 26 '13 at 12:44
  • 7
    keep in mind TelephonyManager.SIM_STATE_READY will fail for CDMA phones that support telephony (e.g. some Verizon phones). So if the `tm.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM` you'll want to check `context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)` to ensure whether it is really a tablet with no telephony, or just a phone with no SIM cards but with telephony. – i Code 4 Food May 21 '14 at 17:12
  • 1
    @iCode4Food Will the PackageManager.FEATURE_TELEPHONY check if the device has a VoIP dialer like Hangouts Dialer? – miguel Apr 01 '15 at 18:41
  • 1
    Actually the simcard check `&& telephonyManager.getSimState()==TelephonyManager.SIM_STATE_READY` adds unneccesary overhead. If the user doesn't have a simcard/or it's not usable for some reason it's much easier to let the standard phone call dialogues handle that. Otherwise you have to write your own error handling and differentiate between tablets and phones without sim to give useful error dialogs. – HopefullyHelpful Sep 14 '16 at 14:58
  • Keep in mind that even without physical telephony support there might be apps installed that can actually handle ACTION_DIAL, e.g. Skype – Till - Appviewer.io Nov 25 '17 at 04:39
9

Intent.ACTION_VIEW worked out better for me because on tablets without a dialer it automatically switched to "Add to Contact".

dt0
  • 713
  • 2
  • 8
  • 18