Calculation problem with timer

Hi @ all,

I have actually a big problem with my timer. I have created a timer in my program:

        //Timer1

        time = new System.Timers.Timer ();
        time.Interval = 1000;
        time.Elapsed += new System.Timers.ElapsedEventHandler(time_action);
        time.Start ();

        cmd_pause.Click += delegate {

            //Timer Stoppen
            if (cmd_pause.Text == "Pause"){
            time.Stop();
                cmd_pause.Text = "Start";
            }
            else
            {
                time.Start();
                cmd_pause.Text = "Pause";
            }

Now I have the problem, I have set the timer to 1 second and if I run the timer so it’s working fine (calculates 1 second) but if I`m deactivating my screen on my S4 so (I have made the test with 1 minute) it calculates only 53 seconds in a real minute.

So, the problem is:
Active screen -> 1 minute (on the timer) is 1 minute on the clock
Deactivated screen (pressed the power button or the phone go’s into standby) -> the timer calculates 7 seconds to less for a minute.

So my question, have I here only an error in the program or have I to set the time of the timer in the status deactivated to another time. If it’s only works the second solution, how I can check the status of the telephone?

Thank You
Withewolf

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    how I can check the status of the telephone?

    Use a Service

  • withewolfwithewolf USMember

    Hi Cheesebaron,

    Thank you for your answer, other solutions for correct the time don’t exists or? because I think is not a good solution to set the time, if the phone it’s on standby to 1020 milliseconds becuase I think on other phones can be also a other time. So this will be the latest solution that I will take.

    Thank You,
    Withewolf

  • CheesebaronCheesebaron DKInsider, University mod

    I have no idea what you just wrote... I feel like there is a language barrier here. Maybe try with Google translate, because your word order in your sentences makes what you write super incoherent.

  • withewolfwithewolf USMember

    Sry, I have only the question if no other solution exists to solf the problem because about me it is not a good way to change the milliseconds of the timer on base of the status of the phone or what you think about this?

    Thank You
    withewolf

  • CheesebaronCheesebaron DKInsider, University mod

    Didn't I tell you to use a Service instead to do background operations?

  • withewolfwithewolf USMember

    Hi,

    yes you did. Has you an example how I can make this, Im very new in the Xamarin programming and I have never worked with a Service.

    thanks a lot
    withewolf

  • withewolfwithewolf USMember

    Hi,

    I have tried to change the code, butt I receive some errors in the service:

    AndroidManifest.xml:
    <?xml version="1.0" encoding="utf-8"?>




    MainActivity.cs:

    using System;
    
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace Service
    {
        [Activity (Label = "Service", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {
            int count = 1;
    
            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);
    
                // 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 {
                    StartService (new Intent (this, typeof(DemoService)));
                };
            }
    
    
        }
    
    }
    
    
    [Service]
    public class DemoService : Service
    {
        //Timer1
    
        time = new System.Timers.Timer ();
        time.Interval = 1000;
        time.Elapsed += new System.Timers.ElapsedEventHandler(time_action);
        time.Start ();
    
        protected void time_action(object sender, System.Timers.ElapsedEventArgs e){
    
            //Update auf UI überneommen wird und dargestellt werden kann
    //      RunOnUiThread(() => {
    
                Button button = FindViewById<Button> (Resource.Id.myButton);
            button.Text = Convert.Tostring(i);
            i = Convert.ToInt32(button.Text) + 1
    
    
                if (i == 60){
                    StopService (new Intent (this, typeof(DemoService)));
                }
    
    //          )};
                }
    
    
    }
    

    Can you please tell me what is the error on my code, I don`t know what I make false?

    Thank You
    withewolf

  • withewolfwithewolf USMember

    Ok problem with service is solved, only new question is how I can take text from a Textview ore write into a Textview every second of the timer (The Textview should display every second 1, 2, 3,...). On service I can not create Textviews to enter the value.

  • CheesebaronCheesebaron DKInsider, University mod

    When the Activity is shown you bind it to the Service to get values from it.

  • withewolfwithewolf USMember

    Super now its working.
    thanks a lot
    withewolf

Sign In or Register to comment.