Xamarin.Android:如何从SMS应用程序返回主应用程序

时间:2017-04-16 14:20:33

标签: android xamarin

我的应用程序使用以下代码从MainActivity调用默认SMS应用程序:

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

默认SMS应用程序已启动,但当我按下“返回”按钮时,我只是关闭应用程序窗口或返回MainActivity窗口。任何人都能解释一下我犯了错误吗?

1 个答案:

答案 0 :(得分:0)

我没有让你的代码工作,我认为它只适用于你拥有的一种消息传递应用程序。我建议使用这种代码来调用短信应用程序(更多关于它的讨论: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);

使用此代码,在短信应用程序中立即按下后退按钮将重新打开您的应用程序。但是如果用户在短信应用程序中做了其他事情,我的经验是它不会回到你的应用程序。我不认为你可以强制另一个应用程序总是回到你的应用程序。如果您不接受,我建议您直接从您的申请中发送短信。

相关问题