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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…