代码以下:java
public void sendMessage(String number) { if (TextUtils.isEmpty(number)) { return; } Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(Constants.SCHEME_SMS, number, null)); context.startActivity(intent); }
异常信息提示以下:android
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=sms:xxxxxxxxxxx } app
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)this
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)spa
at android.app.Activity.startActivityForResult(Activity.java:3424)code
调查以下:blog
1,若是手机中没有能发送短信的app就会报出这样的错get
2,手机中的能发送短信的应用被关闭了(设置-->应用-->app-->关闭);it
解决方法:为了不有的手机没有打开相应文件的app,在startActivity那里作一个try catchio
public void sendMessage(String number) { if (TextUtils.isEmpty(number)) { return; } Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(Constants.SCHEME_SMS, number, null)); try { context.startActivity(intent); } catch(ActivityNotFoundException exception) { Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show(); } }
or 调用系统方法判断是否有对应的app
public void sendMessage(String number) { if (TextUtils.isEmpty(number)) { return; } Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(Constants.SCHEME_SMS, number, null)); PackageManager packageManager = getPackageManager(); List<ResolveInfo>applist = packageManager.queryIntentActivities(intent, 0); if (applist == null || applist.isEmpty()) { Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show(); return; } context.startActivity(intent); }