如何在Android的服务运行CountDownTimer? [英] How to run CountDownTimer in a Service in Android?
问题描述
我想它运行一个CountDownTimer,并在每一个刻度我要显示在活动倒计时还有一些时间后播放声音的服务。
所有的过程都将精在一个单一的活动,但在来电倒计时不能正常工作,这就是为什么我想用一个服务来做到这一点。
任何人可以帮助我吗?
在此先感谢。
更新...
mCountDownTimer =新CountDownTimer(mTimerDuration,1000){
@覆盖
公共无效onTick(长millisUntilFinished){
如果(mTimerDuration大于0){
mDurationCount + = 1000;
showCountDown(
ActivityA.this,
(mSimpleDateFormat.format(mTimerDuration
- mDurationCount)));
如果(mDurationCount == mTimerDuration){
如果(mRepeatTime→1){
startRepeatTimer();
}
finishTimer();
}
}
}
@覆盖
公共无效onFinish(){
}
}。开始();
最简单的方法可能是使用创建活动为您提供广播接收器,并有服务发送广播。下面是一个简单的CountDownTimer服务类的完整列表。
包com.example.cdt;
进口android.app.Service;
进口android.content.Intent;
进口android.os.CountDownTimer;
进口android.os.IBinder;
进口android.util.Log;
公共类BroadcastService延伸服务{
私人最终静态字符串变量=BroadcastService;
公共静态最后弦乐COUNTDOWN_BR =your_package_name.countdown_br;
意图BI =新的意图(COUNTDOWN_BR);
CountDownTimer CDT = NULL;
@覆盖
公共无效的onCreate(){
super.onCreate();
Log.i(TAG,启动定时器......);
CDT =新CountDownTimer(30000,1000){
@覆盖
公共无效onTick(长millisUntilFinished){
Log.i(TAG,倒计时秒时:+ millisUntilFinished / 1000);
bi.putExtra(倒计时,millisUntilFinished);
sendBroadcast(BI);
}
@覆盖
公共无效onFinish(){
Log.i(TAG,计时器完成);
}
};
cdt.start();
}
@覆盖
公共无效的onDestroy(){
cdt.cancel();
Log.i(TAG,计时器已取消);
super.onDestroy();
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
返回super.onStartCommand(意向,标志,startId);
}
@覆盖
公众的IBinder onBind(意向为arg0){
返回null;
}
}
和这里的相关线路从主要活动:
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
startService(新意图(这一点,BroadcastService.class));
Log.i(TAG,启动服务);
}
私人BroadcastReceiver的BR =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
updateGUI(意向); //或任何方法用于更新您的图形用户界面领域
}
};
@覆盖
公共无效onResume(){
super.onResume();
registerReceiver(BR,新的IntentFilter(BroadcastService.COUNTDOWN_BR));
Log.i(TAG,注册broacast接收器);
}
@覆盖
公共无效的onPause(){
super.onPause();
unregisterReceiver(BR);
Log.i(TAG,未注册broacast接收器);
}
@覆盖
公共无效的onStop(){
尝试 {
unregisterReceiver(BR);
}赶上(例外五){
//接收器很可能已停止的onPause()
}
super.onStop();
}
@覆盖
公共无效的onDestroy(){
stopService(新意图(这一点,BroadcastService.class));
Log.i(TAG,停止服务);
super.onDestroy();
}
私人无效updateGUI(意向意图){
如果(intent.getExtras()!= NULL){
长millisUntilFinished = intent.getLongExtra(倒计时,0);
Log.i(TAG,倒计时秒时:+ millisUntilFinished / 1000);
}
}
你还需要在你的清单文件中定义的开始/结束应用程序标记之间的服务。
<服务机器人:名称=。BroadcastService/>
I want a service which runs a CountDownTimer and in every tick I want to show the countdown in a Activity and after some interval play a sound.
All the process are going fine in a single Activity but during incoming call the countdown not working that's why I want to do this using a Service.
Can anybody help me?
thanks in advance.
Update...
mCountDownTimer = new CountDownTimer(mTimerDuration, 1000) {
@Override
public void onTick(long millisUntilFinished) {
if (mTimerDuration > 0) {
mDurationCount += 1000;
showCountDown(
ActivityA.this,
(mSimpleDateFormat.format(mTimerDuration
- mDurationCount)));
if (mDurationCount == mTimerDuration) {
if (mRepeatTime > 1) {
startRepeatTimer();
}
finishTimer();
}
}
}
@Override
public void onFinish() {
}
}.start();
The easiest way is probably to use create a broadcast receiver in your activity and have the service send broadcasts. Here's a full listing for a service class with a simplified CountDownTimer.
package com.example.cdt;
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 = "your_package_name.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(30000, 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");
}
};
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;
}
}
And here are the relevant lines from a main activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this, BroadcastService.class));
Log.i(TAG, "Started service");
}
private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateGUI(intent); // or whatever method used to update your GUI fields
}
};
@Override
public void onResume() {
super.onResume();
registerReceiver(br, new IntentFilter(BroadcastService.COUNTDOWN_BR));
Log.i(TAG, "Registered broacast receiver");
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(br);
Log.i(TAG, "Unregistered broacast receiver");
}
@Override
public void onStop() {
try {
unregisterReceiver(br);
} catch (Exception e) {
// Receiver was probably already stopped in onPause()
}
super.onStop();
}
@Override
public void onDestroy() {
stopService(new Intent(this, BroadcastService.class));
Log.i(TAG, "Stopped service");
super.onDestroy();
}
private void updateGUI(Intent intent) {
if (intent.getExtras() != null) {
long millisUntilFinished = intent.getLongExtra("countdown", 0);
Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
}
}
You'll also need to define the service between the start/end application tags in your manifest file.
<service android:name=".BroadcastService" />
这篇关于如何在Android的服务运行CountDownTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!