在Intent之前检查SMS和DIal支持

时间:2015-05-09 09:17:15

标签: android android-intent sms tablet phone-call

我有这段代码通过 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))); } 发送短信并拨打电话号码:

AVCaptureDevice

它在我的手机中看起来很好,没有崩溃,但在我的平板电脑中不支持短信和通话(没有SIM卡和短信和拨号应用程序)应用程序在调用这些方法时崩溃,所以如何处理那个例外?

3 个答案:

答案 0 :(得分:2)

您必须始终检查Activity是否可以处理您的Intent

假设 Skype 安装在平板电脑上,可以处理调用意图然后启动意图

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
}

同时检查此link

答案 1 :(得分:1)

那应该有用

PackageManager pm = this.getPackageManager();

 if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     System.out.println("Supported");
 } else {
     System.out.println("nope");
 }

答案 2 :(得分:1)

您可以这样处理:

public void Sms(String s){
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", s, null)));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "No app for this", Toast.LENGTH_LONG).show();
    }
}