即使用户关闭应用程序,

时间:2019-05-25 11:08:56

标签: java android timer countdowntimer

我在stackoverflow上找到了一些我认为可以解决我倒数计时器问题的代码。不幸的是,这并没有完全帮助我达到我想要的目标。

问题是我使用服务来监听一个倒数计时器,当倒数计时器完成时,我还保存了一个布尔值,也就是当倒数计时器启动时。保存此布尔值是为了防止用户滥用我的应用程序中的服务。

问题是关闭应用程序后,倒数计时器似乎无法继续运行。因此,我希望countdowntimer在用户关闭应用程序时保持滴答,甚至在countdowntimer完成时更改布尔变量的值,然后保存到sharedprefs中,因此当用户返回应用程序时,我们可以检查是否应延迟用户使用某些功能。

MainActivity

private BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i(TAG,"BroadcastReceiver received");
            updateGUI(intent); // or whatever method used to update your GUI fields
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        registerReceiver(br, new IntentFilter(BroadcastService.COUNTDOWN_BR));
        userUpdateDelay=getBooleanFromSP(this);
        Log.i(TAG, "Registered broadcast receiver");
    }

    @Override
    public void onPause() {
        super.onPause();
        saveBooleanInSP(this,userUpdateDelay);
        unregisterReceiver(br);
        Log.i(TAG, "Unregistered broacast receiver");
    }

    @Override
    public void onStop() {
        try {
            unregisterReceiver(br);
            saveBooleanInSP(this,userUpdateDelay);
        } catch (Exception e) {
            // Receiver was probably already stopped in onPause()
        }
        super.onStop();
    }

    @Override
    public void onDestroy() {
        stopService(new Intent(this, BroadcastService.class));
        saveBooleanInSP(this,userUpdateDelay);
        Log.i(TAG, "Stopped service");
        super.onDestroy();
    }

    private void updateGUI(Intent intent) {
        if (intent.getExtras() != null) {
            long millisUntilFinished = intent.getLongExtra("countdown", 0);
            userUpdateDelay = intent.getBooleanExtra("stopdelay",true);
            Log.i(TAG, "Countdown seconds remaining: " +  millisUntilFinished / 1000);

            if(!userUpdateDelay){
                // StopService, no userDelay anymore!
                // If user hits updatePrice-btn again, startService and set userDelay!
                stopService(new Intent(MainActivity.this, BroadcastService.class));
            }
        }
    }

    public static void saveBooleanInSP(Context _context, boolean value){

        SharedPreferences preferences = _context.getSharedPreferences("test", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("USER_DELAY", value);
        editor.apply();

    }//savePWDInSP()

    public static boolean getBooleanFromSP(Context _context) {

        // TODO Auto-generated method stub
        SharedPreferences preferences = _context.getSharedPreferences("test", Context.MODE_PRIVATE);
        return preferences.getBoolean("USER_DELAY", false);
    }//getPWDFromSP()

BroadcastService

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;

public class BroadcastService extends Service {

    private final static String TAG = "BroadcastService";

    public static final String COUNTDOWN_BR = "gasprices.countdown_br";
    Intent bi = new Intent(COUNTDOWN_BR);

    CountDownTimer cdt = null;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.i(TAG, "Starting timer...");

        cdt = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
                bi.putExtra("countdown", millisUntilFinished);
                sendBroadcast(bi);
            }

            @Override
            public void onFinish() {
                Log.i(TAG, "Timer finished");
                bi.putExtra("stopdelay",false);
                sendBroadcast(bi);
            }
        };

        cdt.start();
    }

    @Override
    public void onDestroy() {

        cdt.cancel();
        Log.i(TAG, "Timer cancelled");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

0 个答案:

没有答案