Ejemplo n.º 1
0
            public void OnServiceConnected(ComponentName name, IBinder service)
            {
                if (service is MainServiceBinder demoServiceBinder)
                {
                    _activity._binder  = demoServiceBinder;
                    _activity._isBound = true;

                    // keep instance for preservation across configuration changes
                    _binder = demoServiceBinder;
                }
            }
Ejemplo 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;
            }
        }
Ejemplo n.º 3
0
 public override IBinder OnBind(Intent intent)
 {
     _binder = new MainServiceBinder(this);
     return(_binder);
 }