Please help me for this exception issues, when click the button send it will return an error.

JunCainJunCain Member ✭✭✭
edited February 27 in Xamarin.Android

Unhandled Exception:

Java.Lang.SecurityException:

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using Android.Telephony;
using System;
using Android.Content;

namespace MhylesOrderingApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private SmsManager _smsManager;
        private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
            var phoneNum = FindViewById<EditText>(Resource.Id.phoneNum);
            var sms = FindViewById<EditText>(Resource.Id.txtSMS);
            _smsManager = SmsManager.Default;

            smsBtn.Click += (s, e) =>
            {
                var phone = phoneNum.Text;
                var message = sms.Text;

                var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
                var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);

                _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
            };


        }
        protected override void OnResume()
        {
            base.OnResume();

            _smsSentBroadcastReceiver = new SMSSentReceiver();
            _smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();

            RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
            RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));
        }

        protected override void OnPause()
        {
            base.OnPause();

            UnregisterReceiver(_smsSentBroadcastReceiver);
            UnregisterReceiver(_smsDeliveredBroadcastReceiver);
        }
    }

    [BroadcastReceiver]
    public class SMSSentReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            switch ((int)ResultCode)
            {
                case (int)Result.Ok:
                    Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.GenericFailure:
                    Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.NoService:
                    Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.NullPdu:
                    Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.RadioOff:
                    Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
                    break;
            }
        }
    }

    [BroadcastReceiver]
    public class SMSDeliveredReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            switch ((int)ResultCode)
            {
                case (int)Result.Ok:
                    Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
                    break;
                case (int)Result.Canceled:
                    Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
                    break;
            }
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1" 
          android:versionName="1.0" 
          package="MhylesOrderingApp.MhylesOrderingApp" 
          android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme">
  </application>
</manifest>

activity_main.axml

<EditText 
        android:id="@+id/phoneNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
    <EditText 
        android:id="@+id/txtSMS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
    <Button
        android:id="@+id/btnSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send" />

Best Answers

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    maybe you have to set some "permissions"

  • JunCainJunCain Member ✭✭✭

    but I already set a sms permission in my manifest file, do I need to set other permission except that ?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    maybe a runtime one, I don't know, never sent an SMS

  • JunCainJunCain Member ✭✭✭

    smsBtn.Click += (s, e) =>
    {
    ...
    var piSent = PendingIntent.GetBroadcast(this, 0, intentFilter, 0);
    ... //intentFilter error Argument 3: cannot convert from 'Android.Content.IntentFilter' to 'Android.Content.Intent'
    };

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited February 28

    Sorry for my mistake ,
    Change the code int MainActivity :

    IntentFilter intentFilter = new IntentFilter("SMS_SENT");
    intentFilter.addAction("android.permission.SEND_SMS");
    ...
    smsBtn.Click += (s, e) =>
                {
                    ...
                    var piSent = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_SENT"), 0);
                    ...
                };
    

    and then deploy to the mobile device to test.

  • JunCainJunCain Member ✭✭✭

    I think there is something wrong, how about the intentFilter?
    I used this but the error exception still there.

    IntentFilter intentFilter = new IntentFilter("SMS_SENT");
                intentFilter.AddAction("android.permission.SEND_SMS");
     smsBtn.Click += (s, e) =>
                {
                    var phone = phoneNum.Text;
                    var message = sms.Text;
    
                    var piSent = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_SENT"), 0);
                    var piDelivered = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_DELIVERED"), 0);
    
                    _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                };~~~~
    
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited February 28

    Please delete the following code in OnResume()

    RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
    

    then change the code in OnCreate()

    IntentFilter intentFilter = new IntentFilter("SMS_SENT");
    intentFilter.AddAction("android.permission.SEND_SMS");
    
    RegisterReceiver(_smsSentBroadcastReceiver, intentFilter); // add RegisterReceiver
    smsBtn.Click += (s, e) =>
                {
                    var phone = phoneNum.Text;
                    var message = sms.Text;
    
                    var piSent = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_SENT"), 0);
                    var piDelivered = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_DELIVERED"), 0);
    
                    _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                };
    

    I attached the picture of my MainActivity.cs , you can refer to it.

  • JunCainJunCain Member ✭✭✭
    edited February 28

    I think there is something wrong in my machine coz, errors still there after all the changes, Even if I grab some code in github own by other programmers and run it into my pc, it will return the same error like my own code have.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have you deployed the code to the mobile device to test rather than virtual device.

  • JunCainJunCain Member ✭✭✭

    there is some error when I'm try to run into my device, I think because I am using axml file UI.. The error is like this MainActivity.cs: 'Resource.Id' does not contain a definition for 'btnSend' and so on.

  • JunCainJunCain Member ✭✭✭

    Now I run it into a real device but the error is still there.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Would you mind sharing a basic demo that could reproduce this issue?

  • JunCainJunCain Member ✭✭✭

    Ok, I will post my project here...

  • JunCainJunCain Member ✭✭✭

    Posted...

  • JunCainJunCain Member ✭✭✭

    Thanks a lot Sir.

  • JunCainJunCain Member ✭✭✭

    I have another problem Sir i don't know how to put a loading bar or a circular loading while the SMS is on processes and after the loading stop the toast message will pop up?

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have you solved the issue?

  • JunCainJunCain Member ✭✭✭

    yeah, I used ProgressDialog..

Sign In or Register to comment.