How to run a timer in the background ?

前端 未结 1 1359
情话喂你
情话喂你 2021-01-21 15:28

I have code for a timer , but if i change fragment the timer was reset to 00:00 i want that timer still count after i click stop or pause or that mean this timer still count in

相关标签:
1条回答
  • 2021-01-21 16:20

    Problem :- When you switch fragment and come back to Timer Fragment, the onCreate lifecycle callback will be called and that gone reset your timer in

    customHandler.postDelayed(updateTimerThread, 0);

    Solution :- Move timer logic to Activity class but activity will be recreate in orientation change so move timer logic into Application class :-

    Drop this class in you project package

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.app.Application;
    import android.os.Handler;
    import android.widget.Toast;
    
    public class App extends Application {
    
        public static App appInstance;
        private SimpleDateFormat dateFormat;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            appInstance = this;
    
            dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }
    
        public void afficher() {
            Toast.makeText(getBaseContext(), dateFormat.format(new Date()), 300).show();
            handler.postDelayed(runnable,1000);
        }
    
        public void startTimer() {
            runnable.run();
        }
    
        public void stopTimer() {
            handler.removeCallbacks(runnable);
        }
    
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                afficher();
            }
        };
    
    }
    

    Set your Application name to App class like this below (android name property of application) :-

       <application
            android:name="com.masterdetail_webview.App"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.masterdetail_webview.TimertestActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    

    Now you can start and stop timer simply like this from anywhere

    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                App.appInstance.startTimer();
    
            }
        });
    
        findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                App.appInstance.stopTimer();
    
            }
        });
    

    Adding AppController

    0 讨论(0)
提交回复
热议问题