0

My application uses the following code for calling default SMS application from the MainActivity:

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
        {
            string defaultSmsPackageName = Telephony.Sms.GetDefaultSmsPackage(this);
            Intent intent = new Intent(Intent.ActionMain);
            intent.AddCategory(Intent.CategoryDefault);
            intent.SetType("vnd.android-dir/mms-sms");
            if (defaultSmsPackageName != null)
                intent.SetPackage(defaultSmsPackageName);
            StartActivity(intent);
        }
        else
        {
            Intent intent = new Intent(Intent.ActionMain);
            intent.AddCategory(Intent.CategoryDefault);
            intent.SetType("vnd.android-dir/mms-sms");
            StartActivity(intent);
        }

Default SMS application started, but when I pressed Back button I just close application window instead or returning to the MainActivity window. Can anybody explain me where I made error?

Hermann
  • 247
  • 2
  • 18

1 Answers1

0

I did not get your code to work, I think it only works with one kind of messaging app that you have. I would suggest using this kind of code to invoke sms application (More discussion about it here: Sending SMS via an Intent and know if the SMS has been sent or not):

Intent intent = new Intent(Intent.ActionView);
intent.SetData(Android.Net.Uri.Parse("smsto:" + phoneNumber));
intent.PutExtra("address", phoneNumber);
intent.PutExtra("sms_body", messageBody);
intent.PutExtra("exit_on_sent", true);
StartActivity(intent);

With this code pressing the back button immediately in the sms application will reopen your application. But if the user does something else in the sms application my experience is that it won't go back to your application. I don't think you can force another application to always go back to your application. If that is not acceptable to you I would suggest looking into sending the sms directly from your application.

Community
  • 1
  • 1
hamalaiv
  • 828
  • 2
  • 9
  • 18
  • @hamalaviv, my goal is to delete incoming SMS by hands then return to my application. My code is working properly under Android 4.0.9, but doesn't work under Android 6.0 – Hermann Apr 18 '17 at 05:43