如何启动计时器3小时甚至应用程序关闭或从Android中的活动回来

时间:2016-11-29 05:42:46

标签: android timer countdowntimer

一旦计时器启动它就不能停止3个小时。如果我点击反压计时器stoped.I我不知道如何暂停和恢复计时器作为textview.Please检查我的代码。

TextView timer;

SharedPreferences mpref;

SharedPreferences.Editor ed;
String output;
MyCount counter;
long seconds;

long millisFinished;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_start__test2);
    mpref=getSharedPreferences("com.example.bright", Context.MODE_PRIVATE);

      timer = (TextView) findViewById(R.id.timer);



    //startService(new Intent(this, MyService.class));
    counter = new MyCount(10800000, 1000);

    counter.start();


}

countDownTimer方法

   public class MyCount extends CountDownTimer {
    Context mContext;


   public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        Log.e("timeeeee", millisInFuture + "");
    }


    public void onTick(long millisUntilFinished) {
        Log.e("time",millisUntilFinished+"");

        millisFinished = millisUntilFinished;
        timer.setText(formatTime(millisUntilFinished));
    /*    String timer_str = timer.getText().toString();
        //SharedPreferences sp=

        ed = mpref.edit();
        ed.putString("time", timer_str);
        ed.commit();*/

        if (seconds == 0) {

        }
    }



    public void onFinish() {
        Toast.makeText(getApplicationContext(), "Time Up", Toast.LENGTH_LONG).show();
    }


}

@Override
protected void onResume() {
    super.onResume();
  /*//  Log.e("valueeeee",millisFinished+"");
   // new MyCount(millisFinished,1000);
   // Log.e("value",millisFinished+"");*/
  //counter

}

@Override
public void onDestroy() {
    super.onDestroy();
    // counter.cancel();
}

//================================================================================Time format

public String formatTime(long millis) {
    output = "";
    seconds = millis / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;

    seconds = seconds % 60;
    minutes = minutes % 60;
    hours = hours % 60;

    String secondsD = String.valueOf(seconds);
    String minutesD = String.valueOf(minutes);
    String hoursD = String.valueOf(hours);

    if (seconds < 10)
        secondsD = "0" + seconds;
    if (minutes < 10)
        minutesD = "0" + minutes;

    if (hours < 10)
        hoursD = "0" + hours;

    output = hoursD + " : " + minutesD + " : " + secondsD;

    return output;
}

请检查我的代码

2 个答案:

答案 0 :(得分:2)

您需要使用服务,以便即使应用程序已关闭/销毁,计时器也会运行。请尝试以下

    public class TimerService extends Service {
        private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        private Context mContext;

        public IBinder onBind(Intent intent) 
        {
            return null;
        }

        public void onCreate() 
        {
            super.onCreate();
            mContext = this; 
            startService();
        }

        private void startService()
        {           
            scheduler.scheduleAtFixedRate(runner, 0, 3, TimeUnit.HOURS);
        }

        final Runnable runner = new Runnable() {
            public void run() 
            {
                mHandler.sendEmptyMessage(0);
            }
        }    

        public void onDestroy() 
        {
            super.onDestroy();
            Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
        }

        private final Handler mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                //do what ever you want as 3hrs is completed
            }
        };    
    }

答案 1 :(得分:0)

如果您在活动范围内创建对象,则一旦活动因活动生命周期而消失,它们将被处置。 运行长后台任务的解决方案是IntentService提供的服务。 你可以在这里阅读更多相关信息:

https://developer.android.com/training/run-background-service/create-service.html

祝你好运!