Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

how can i build an app that starts when the phone recieves a call i have tried broadcast reciever

mambomambo Member ✭✭

here is some of my code
[BroadcastReceiver(Enabled =true, Exported =true)]
[IntentFilter(new[] { TelephonyManager.ActionPhoneStateChanged })]
public class Reciever : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
if(intent.Action == TelephonyManager.ActionPhoneStateChanged)
{
var state = intent.GetStringExtra(TelephonyManager.ExtraState);
if(state == TelephonyManager.ExtraStateRinging)
{
var number = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
Toast.MakeText(context, number, ToastLength.Short).Show();
}
}
}
}

Answers

  • LeoZhuLeoZhu Member ✭✭✭

    After you listen for the call, if it's the number you want to listen for, you can just start MainaActivity,like your codes above :

    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        if(intent.Action == TelephonyManager.ActionPhoneStateChanged)
          {
            var state = intent.GetStringExtra(TelephonyManager.ExtraState);
            if(state == TelephonyManager.ExtraStateRinging)
              {
                 var number = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
                 Toast.MakeText(context, number, ToastLength.Short).Show();
                 if(number == yournumber){
                    Intent intent = new Intent(context,typeof(MainActivity));
                    intent .SetFlags(ActivityFlags.NewTask);
                    context.StartActivity()
                  }
             }
        }
    }
    
  • mambomambo Member ✭✭

    Thanks man but still it is not working I have attached the project if you can help me its school work

  • LeoZhuLeoZhu Member ✭✭✭

    what it said after running the codes ? i just could give you a thought and direction

  • mambomambo Member ✭✭

    thanks man the codde deployed successfully but when i call the application does not respond

Sign In or Register to comment.