ActivityNotFoundException for sms intent

0

My sendSms function has try and catch blocks. Try block is default device sms application. catch block is other sms applications which set as current sms application for example Google SMS app.

Some phones(generally API version is 19) have this error when running this sendSms code.

My error log:

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)

My code:

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);
                        }
                    }
    }
android
android-intent
sms
asked on Stack Overflow Feb 19, 2018 by propoLis • edited Feb 19, 2018 by propoLis

2 Answers

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;
    }
answered on Stack Overflow Feb 19, 2018 by Gautam Kushwaha • edited Oct 21, 2019 by marcolz
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);

First time please remove setFlags and try my source.Also share your gradle file's source

answered on Stack Overflow Feb 19, 2018 by BekaKK

User contributions licensed under CC BY-SA 3.0