我的sendSms函数尝试捕获块。 Try
块是默认设备短信应用程序。 catch
阻止是其他短信应用程序,它们设置为当前短信应用程序,例如Google SMS应用程序。
某些手机(通常API版本为19)在运行此sendSms
代码时会出现此错误。
我的错误日志:
Exception android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=sms:xxxxxxxxxxxxxxx flg=0x10000000 (has extras) }
android.app.Instrumentation.checkStartActivityResult (Instrumentation.java:1638)
android.app.Instrumentation.execStartActivity (Instrumentation.java:1430)
android.app.ContextImpl.startActivity (ContextImpl.java:1135)
android.app.ContextImpl.startActivity (ContextImpl.java:1117)
android.content.ContextWrapper.startActivity (ContextWrapper.java:311)
android.view.View.performClick (View.java:4463)
android.view.View$PerformClick.run (View.java:18794)
android.os.Handler.handleCallback (Handler.java:808)
android.os.Handler.dispatchMessage (Handler.java:103)
android.os.Looper.loop (Looper.java:193)
android.app.ActivityThread.main (ActivityThread.java:5299)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:825)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:641)
dalvik.system.NativeStart.main (NativeStart.java)
我的代码:
private void sendSms(String phoneNumber){
try {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", getContext().getResources().getString(R.string.inviteSms));
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.putExtra("address", phoneNumber);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(sendIntent);
} catch (Exception e) {
String SMS_DEFAULT_APPLICATION = "sms_default_application";
String defaultApplication = Settings.Secure.getString(getContext().getContentResolver(), SMS_DEFAULT_APPLICATION);
PackageManager pm = getContext().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(defaultApplication);
if (intent != null) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNumber, null));
smsIntent.putExtra("sms_body", getContext().getString(R.string.inviteSms));
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(smsIntent);
}
}
}
答案 0 :(得分:2)
if(getDefaultSmsAppPackageName()!=null){
Uri smsUri= Uri.parse("smsto:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
public static String getDefaultSmsAppPackageName(@NonNull Context context) {
String defaultSmsPackageName;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
return defaultSmsPackageName;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_DEFAULT).setType("vnd.android-dir/mms-sms");
final List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty())
return resolveInfos.get(0).activityInfo.packageName;
}
return null;
}
答案 1 :(得分:1)
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
StringBuilder uri = new StringBuilder("sms:" + mobilenumber);
sendIntent.putExtra("sms_body", "");
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.setData("");
startActivity(sendIntent);
首次请删除setFlags并尝试我的源码。也可以分享您的gradle文件来源