倒数计时器在后台更新 ui

Countdown timers in the background updating the ui

我有一个倒计时计时器,可以倒计时用户设置的当前时间。虽然,我希望能够访问其他应用程序,或者在倒数计时器 运行 的同时进入设备的主屏幕,并继续使用倒计时剩余的计时器更新 textview。有什么办法吗?我尝试实现下面的服务,但我不知道这是否是完成此任务的正确方法,因为我确实收到了一个错误。

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_down);



//        if (savedInstanceState != null){
//
//        }

        titleTextView = (TextView)findViewById(R.id.titleTextView);
        timeTextView = (TextView)findViewById(R.id.timeTextView);


        Bundle getAlarmInfo = getIntent().getExtras();
        titleOfAlarm = getAlarmInfo.getString("Title");
        totalTime = getAlarmInfo.getString("totalTime");

         number = new BigDecimal(totalTime).toBigInteger();


        actualTimeFiniliazedInMilliSeconds = number.intValue();
        titleTextView.setText(titleOfAlarm);

//        countDown = new CountDownTimer(actualTimeFiniliazedInMilliSeconds, timeInterval);

//        new UpdateCountDownTime().execute();


        countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
        countDown.start();
//        startService(new Intent(getApplicationContext(), CountDown.class));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_count_down, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public class CountDownTime extends CountDownTimer {


        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public CountDownTime(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {



            long millis = millisUntilFinished;
            hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));


            timeTextView.setText(hms);
        }

        @Override
        public void onFinish() {
            Intent goBack = new Intent(CountDownAct.this, ListOfAlarms.class);
        startActivity(goBack);
            finish();
        }




    }

    @Override
    protected void onPause() {
        super.onPause();
        timeTextView.setText(hms);





    }



//    public class CountDown extends Service{
//
//        public  CountDown () {
//
//        }
//
//        @Override
//        public IBinder onBind(Intent intent) {
//            return null;
//        }
//
//        @Override
//        public void onCreate() {
//            super.onCreate();
//
//            countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
//            countDown.start();
//        }
//    }

其实你需要Alarm Manager + Broadcast Receiver

您的警报管理器负责倒计时,而您的广播接收器将等待警报管理器信号。

编辑

完整代码可以在 GUI updater 存储库中下载。

示例代码段

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.github.ecobin.gui_refresh_by_time" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".UpdateService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>

主要活动

    public class MainActivity extends Activity {

        public static final String[] WORD_BANK = {"Hello World!", "Ice cream", "Soda", "Cake", "Apple"};
        public static final int TIMER = 10; // 10 seconds timer.
        public TextView textView;

        IntentFilter fil;
        BroadcastReceiver receiver;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView) findViewById(R.id.hello_world);

            fil = new IntentFilter("mybroadcast");
            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.i(">>>>>>>>>>>", "Updating UI!");
                    updateGUI();
                }
            };

            AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent in = new Intent(this, UpdateService.class);
            PendingIntent pending = PendingIntent.getService(this, 1234, in, 0);

            manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000 * TIMER, 1000 * TIMER, pending);
        }

        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(receiver);
        }

        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(receiver, fil);
        }


// MORE CODE HERE......

        public void updateGUI() {
            int randomNumber = (int) (Math.random() * WORD_BANK.length);
            textView.setText(WORD_BANK[randomNumber]);
        }
    }

您需要使用服务和广播接收器。 我已经为你创建了一些example for broadcast receiver

检查一下,如果有问题请告诉我。

创建服务:TimerService.java

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Initialize and start the counter
        countDown = new HokusFocusCountDownTimer(timeDifference, 1000);

            countDown.start();

        return super.onStartCommand(intent, flags, startId);
    }

在MainActivity.java

/*
    * This is used to receive the updated time from broadcast receiver
    * */
    private final BroadcastReceiver timeBroadCaster = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //method used to update your GUI fields
            updateGUI(intent);
        }
    };

    /**
     * update IU with changing timer of Focus Hour
     *
     * @param intent Focus hour timer value
     */
    private void updateGUI(Intent intent) {

        AppLog.showLog("Update UI", "Timer Stopped");

        if (intent.getExtras() != null) {

            String focusHMS = intent.getStringExtra(Constants.HOCUS_FOCUS_TIMER);
            focusMilliSecond = intent.getLongExtra(Constants.HOCUS_FOCUS_TIMER_MILLI_SECOND, 0);
            txtFocusHourTimer.setText("" + focusHMS);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        registerReceiver(timeBroadCaster, new IntentFilter(
                HokusFocusCountDownTimer.COUNTDOWN_BROADCAST_RECEIVER));


    }

    @Override
    public void onPause() {
        super.onPause();
        try {
            unregisterReceiver(timeBroadCaster);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

希望我的回答对您有所帮助