Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中启动SMS意图的防错方法_Android_Android Intent_Sms - Fatal编程技术网

Android中启动SMS意图的防错方法

Android中启动SMS意图的防错方法,android,android-intent,sms,Android,Android Intent,Sms,在我的Android应用程序中,我使用以下代码启动消息应用程序并为文本消息填写默认文本: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("sms:"+USERS_PHONE_NUMBER)); intent.putExtra("sms_body", "DUMMY TEXT"); startActivity(intent); 这在大多数情况下都有效。但不幸的是,在某些设备上,我收到以下错误消息:

在我的Android应用程序中,我使用以下代码启动消息应用程序并为文本消息填写默认文本:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"+USERS_PHONE_NUMBER));
intent.putExtra("sms_body", "DUMMY TEXT");
startActivity(intent);
这在大多数情况下都有效。但不幸的是,在某些设备上,我收到以下错误消息:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=sms:+XXXXXXXXXX (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1510)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
at android.app.Activity.startActivityForResult(Activity.java:3131)
at android.app.Activity.startActivity(Activity.java:3237)
显然,我创造的意图无法处理

  • 我的短信意向码有错误吗?
  • 如果无法处理意图,如何防止应用程序崩溃?
我应该使用PackageManager.QueryInputActivities()还是有其他解决此问题的方法


提前谢谢

我还没有具体尝试过这个意图,但最简单的方法可能是添加try-and-catch块

try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Display some sort of error message here.
}
因为你不能指望特定的安卓设备有消息应用程序(例如一些平板电脑没有电话服务),你必须做好准备


一般来说,在启动外部活动时,这是一个很好的做法,以避免应用程序崩溃。

以下是打开手机号码预填充的SMS活动的代码 必须发送的短信。这在模拟器和设备上都能很好地工作

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber); 

下面是我用来在Android上安全打开活动的方法,如果没有找到活动,我会给用户一些反馈

public static void safeOpenActivityIntent(Context context, Intent activityIntent) {

        // Verify that the intent will resolve to an activity
          if (activityIntent.resolveActivity(context.getPackageManager()) != null) {
              context.startActivity(activityIntent);
          } else {
              Toast.makeText(context, "app not available", Toast.LENGTH_LONG).show();
          }
}

(我想我是从youtube上的一个谷歌开发者视频中得到的,但是现在我找不到视频了……

非常感谢!我不能说它是否有效,因为错误很少发生。但作为一个例外,“尝试捕捉”应该做这项工作,对吗?在我的例子中,“Intent.ACTION\u VIEW”和“Intent.ACTION\u SENDTO”之间有什么区别吗?我们在应用程序中使用了try-catch方法,它确实有效。就我个人而言,我以前使用过ACTION_SENDTO,它很有效。我想在大多数情况下,它的行为都是一样的(在我的手机上也是一样),但我不确定。如果您想发送消息,最好使用ACTION\u SENDTO