Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

android - Cannot Receive sms

I want to receive a message from my application,I've followed a few post discussing about the same topic,tried reproducing the same thing but cant receive a message on my app.When I receive a message a toast pops up ,after that I can click the button and I should see a message on textview and a toast pop again .Can anyone help me point where Im wrong ? My MainActivity ,Manifest and SmsReciver files are given below :

//----------------- MainActivity ----------------------

  [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
  public class MainActivity : AppCompatActivity
 {
   private intentReceiver _receiver = new intentReceiver();
   private IntentFilter intentFilter;
   protected override void OnCreate(Bundle savedInstanceState)

 {
    base.OnCreate(savedInstanceState);
    // Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);

    TextView translatedPhoneWord = FindViewById<TextView>(Resource.Id.TranslatedPhoneword);
    Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);


    intentFilter = new IntentFilter();
    intentFilter.AddAction("SMS_RECEIEVED_ACTION");


    translateButton.Click += (s, e) =>
    { 

        translatedPhoneWord.Text = _receiver.message;
        Toast.MakeText(ApplicationContext, _receiver.address + ", " + _receiver.message, ToastLength.Short).Show();     //showing a Toast again 

    };

//--------------------- SmsReciever class -----------------------

   [BroadcastReceiver]
   [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" },Priority= (int)IntentFilterPriority.HighPriority)]
  public class intentReceiver : BroadcastReceiver
{
 public string address = "";
 public string message = "";

 public override void OnReceive(Context context, Intent intent)
{

    try
    {

        if (intent.HasExtra("pdus"))
        {
            var smsArray =
            (Java.Lang.Object[])intent.Extras.Get("pdus");

            foreach (var item in smsArray)
            {
                var sms = SmsMessage.CreateFromPdu((byte[])item);
                message += sms.MessageBody;
                address = sms.OriginatingAddress;
                Toast.MakeText(Application.Context, "From :" + address + "Message :" + message,      ToastLength.Long).Show();
            }


            Intent BroadcastIntent = new Intent();
            BroadcastIntent.SetAction("SMS_RECEIEVED_ACTION");
            BroadcastIntent.PutExtra("message", "From :" + address + "Message :" + message);
            context.SendBroadcast(BroadcastIntent);

        }
    }
    catch (Exception e)
    {
        Toast.MakeText(Application.Context, "Error" + e,ToastLength.Long).Show();
    }

}

}

// --------------- Manifest File ----------------------

 uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
 uses-permission android:name="android.permission.SEND_SMS"
 uses-permission android:name="android.permission.RECIEVE_SMS"
 uses-permission android:name="android.permission.READ_SMS"
 uses-permission android:name="android.permission.BROADCAST_SMS"
 uses-permission android:name="android.permission.WRITE_SMS"
 uses-permission android:name="android.permission.VIBRATE"
question from:https://stackoverflow.com/questions/65913544/cannot-receive-sms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...