Book Image

Cross-platform UI Development with Xamarin.Forms

By : Paul Johnson
Book Image

Cross-platform UI Development with Xamarin.Forms

By: Paul Johnson

Overview of this book

Table of Contents (22 chapters)
Cross-platform UI Development with Xamarin.Forms
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
In the Beginning…
Index

Setting up your Android code


Setting up the connectivity code in Android is a two-step procedure:

  1. The first is the creation of BroadcastReceiver.

  2. The second is calling this receiver into being.

Broadcast whatcha-ma-call-it?

When something happens on the hardware level, Android broadcasts a message that any code can listen to. In order to listen to the message, a broadcast receiver is used. In the case of the connectivity broadcast receiver, the code is simple enough:

[BroadcastReceiver]
public class Connectivity : BroadcastReceiver
{
  public override void OnReceive(Context context, Intent intent)
  {
    var extras = intent.Extras;
    
    using (var info = extras.GetParcelable("networkInfo") as NetworkInfo)
    {
      var state = info.GetState();
      
      var result = state == NetworkInfo.State.Connected || state == NetworkInfo.State.Connecting;
      
      // store the online state in the internal settings system
      
      ConfigUtils.SaveSetting("online", result, SettingType.Bool...