Android 查看短信和拨号支持,然后再试一次

Android 查看短信和拨号支持,然后再试一次,android,android-intent,sms,tablet,phone-call,Android,Android Intent,Sms,Tablet,Phone Call,我使用此代码通过Intent发送短信并拨打电话: 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(Inte

我使用此代码通过
Intent
发送短信并拨打电话:

 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)));
}
它在我的手机上看起来不错,没有崩溃,但在我的平板电脑上,它不支持短信息和通话(没有sim卡、短信息和拨号应用),当调用这些方法时,应用程序崩溃,那么如何处理这个异常呢?

应该可以

PackageManager pm = this.getPackageManager();

 if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     System.out.println("Supported");
 } else {
     System.out.println("nope");
 }
这应该行得通

PackageManager pm = this.getPackageManager();

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

您可以这样处理它:

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();
    }
}

您可以这样处理它:

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();
    }
}

您必须始终检查是否是可以处理您的
意图的
活动

假设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
}

同时检查此项

您必须始终检查是否是可以处理您的
意图的
活动

假设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
}
也检查一下这个