Search code examples
c#xamarinxamarin.androidandroid-emulatorandroid-permissions

Android emulation isn't requiring any permissions


I am learning some stuff for a course, and am emulating Android 8.1 API 27, but have ran into some weird behavior.

It is a simple app, which just aims to allow the user to send an SMS message, or display it to the screen in a different activity.

using System;
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using AlertDialog = Android.App.AlertDialog;

namespace EmailSender
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            var sendText = FindViewById<Button>(Resource.Id.sendSMSbutton);
            var displayMessage = FindViewById<Button>(Resource.Id.displayMessageButton);
            var recipient = FindViewById<EditText>(Resource.Id.email);
            var message = FindViewById<EditText>(Resource.Id.message);


            sendText.Click += (sender, args) =>
            {

                string url = string.Format("smsto:{0}", recipient.Text);
                var uri = Android.Net.Uri.Parse(url);
                var intent = new Intent(Intent.ActionSendto, uri);
                intent.PutExtra("sms_body", message.Text);
                StartActivity(intent);

            };

            displayMessage.Click += (sender, args) =>
            {
                var intent = new Intent(this, typeof(MessageDisplayActivity));
                intent.PutExtra("message", message.Text);
                StartActivity(intent);
            };
        }
    }
}

The primary issue from my end is that the app should be requiring the SEND_SMS permission according to our documentation. However it isn't requiring the permission at all. Regardless of me having the permission turned on or off, the app works as expected and opens the SMS interface.

I am not sure if this could be down to me having incorrect setup the emulator, or similar, but if anyone could open my eyes slightly to how I am ignoring this permission I would be really grateful.

Thank you in advance!


Solution

  • There are two methods to send message:

    1.like your above ,call up the system SMS function:

    var intent = new Intent(Intent.ActionSendto, uri);
    ...
    StartActivity(intent);
    

    in this way your application does not need to request this permission.(the same as Camera)

    2.Call the system SMS interface to send messages directly:

    SmsManager sms = SmsManager.Default;
    sms.SendTextMessage(...);
    

    this way,you should request SEND_SMS permission