Esempio n. 1
0
        protected override void OnStart()
        {
            base.OnStart();

            var serviceIntent = new Intent(Application.Context, typeof(MainService));

            _mainServiceConnection = new MainServiceConnection(this);
            BindService(serviceIntent, _mainServiceConnection, Bind.AutoCreate);
        }
Esempio n. 2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById <Button>(Resource.Id.myButton);

            button.Click += delegate { button.Text = $"{_count++} clicks!"; };

            var startButton = FindViewById <Button>(Resource.Id.startService);

            startButton.Click += (sender, args) =>
            {
                Toast.MakeText(Application.Context, "Trying to start service", ToastLength.Long).Show();
                StartService(new Intent(Application.Context, typeof(MainService)));
            };

            var stopButton = FindViewById <Button>(Resource.Id.stopService);

            stopButton.Click += (sender, args) =>
            {
                Toast.MakeText(this, "Trying to stop service", ToastLength.Long).Show();
                StopService(new Intent(Application.Context, typeof(MainService)));
            };

            var callService = FindViewById <Button>(Resource.Id.callService);

            callService.Click += delegate {
                if (_isBound)
                {
                    RunOnUiThread(() => {
                        var text = _binder.GetDemoService().GetText();
                        Toast.MakeText(Application.Context, text, ToastLength.Long).Show();
                    }
                                  );
                }
            };

            var manualRun = FindViewById <Button>(Resource.Id.manualRun);

            manualRun.Click += delegate {
                if (_isBound)
                {
                    RunOnUiThread(() => {
                        _binder.GetDemoService().RunLogic();
                    }
                                  );
                }
            };

            // restore from connection there was a configuration change, such as a device rotation
            _mainServiceConnection = LastNonConfigurationInstance as MainServiceConnection;

            if (_mainServiceConnection != null)
            {
                _binder = _mainServiceConnection.Binder;
            }
        }