Search code examples
androidandroid-intentsmstabletphone-call

Check SMS and DIal support before Intent


I have this code to send SMS via Intent and to call a number :

 public void Call(String s) {//Appeler
    String url = "tel:" + s;
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
    startActivity(intent);
}
public void Sms(String s){
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", s, null)));
}

it looks fine in my phone , no crash , but in my tablet which doesn't support SMS and call (doesn't have sim card and sms and dial app) the app crashes when calling these methods , so how to handle that exception ?


Solution

  • You must always check whether the is Activity which could handle your Intent

    Suppose Skype is install on tablet that can handle your call intent then launch intent

    PackageManager packageManager = getPackageManager();
    List activities = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafe = activities.size() > 0; 
    if(isIntentSafe)
    {
     // call Call(546) or Sms(1235)
    }else{
    // No activity is present  to handle  your intent
    }
    

    Also check this link