gpt4 book ai didi

android - 短信 Intent 的 ActivityNotFoundException

转载 作者:行者123 更新时间:2023-12-04 23:56:44 27 4
gpt4 key购买 nike

我的 sendSms 函数有 try 和 catch block 。 Try block 是默认设备短信应用程序。 catch block 是设置为当前短信应用程序的其他短信应用程序,例如谷歌短信应用程序。

部分手机(一般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);
}
}
}

最佳答案

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

关于android - 短信 Intent 的 ActivityNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48866897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com