Exemple #1
0
        // https://stackoverflow.com/questions/19569546/background-service-without-activity
        // https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
        // https://developerlife.com/2017/07/10/android-o-n-and-below-component-lifecycles-and-background-tasks/
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Android.Support.V7.Widget.Toolbar toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            _buttonRefresh            = FindViewById <FloatingActionButton>(Resource.Id.fab);
            _buttonRefresh.Visibility = ViewStates.Visible;
            _buttonRefresh.Click     += (s, e) => Update();


            _logView    = (TextView)FindViewById(Resource.Id.logtext);
            _scrollView = (ScrollView)FindViewById(Resource.Id.scroller);

            this.Info("Calling StartDroidNodeServiceIfNotRunning()...");
            StartDroidNodeServiceIfNotRunning();
            _nodeServiceConnection = new NodeServiceConnection();
            this.Info("Calling BindService()...");
            bool isBindServiceResultTrue = BindService(new Intent(this, typeof(NodeService)), _nodeServiceConnection, Bind.None);

            if (!isBindServiceResultTrue)
            {
                this.Info("BindService() returned false, returning!");
                return;
            }
            this.Info("BindService() returned true.");
        }
Exemple #2
0
        void Update()
        {
            // TODO: move the linebuffer into the service,
            // so that we can use the default activity mode. A new instance can then also Bind to the current generation of the service. Then, the reference to the bound service will not become invalid.
            try
            {
                this.Info("Fetching log...");
                NodeController controller = _nodeServiceConnection.NodeControllerFactory.Value;
                if (controller != null)
                {
                    var log = controller.GetLog();
                    if (!string.IsNullOrEmpty(log))
                    {
                        var oldText  = _logView.Text;
                        var oldLines = CountLines(oldText);
                        if (oldLines > 200)
                        {
                            var shorter = DeleteLines(oldText, 100);
                            var newText = shorter + log;
                            _logView.Text = newText;
                            this.Info("Truncated log...");
                        }
                        else
                        {
                            _logView.Text = oldText + log;
                        }

                        _scrollView.FullScroll(FocusSearchDirection.Down);
                    }
                }
            }
            catch (Exception e)
            {
                this.Info($"Error in Update(): {e.Message}");
            }
            finally
            {
                _nodeServiceConnection = null;
                //GC.Collect(0);
            }
        }