Android:后台服务中的秒表(计时器)

时间:2017-01-03 16:38:09

标签: android android-service

我想制作一个能够打入并打出功能的Android应用程序。场景是当用户进入应用程序时它进入其任务并按下打卡按钮,当按下按钮时按下当前日期和时间保存在本地数据库中并且计时器正在后台运行,即使我关闭了应用程序但问题是它当我关闭一个应用程序并且再次启动计时器从开始开始时,无法在后台运行。

如何确定我的服务正在运行并获取该数据?

MainActivity.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button startButton;
private Button pauseButton;

private TextView timerValue;

Intent intent;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    startButton = (Button) findViewById(R.id.startButton);
    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            intent = new Intent(MainActivity.this, MyService.class);
            startService(intent);
            registerReceiver(broadcastReceiver, new IntentFilter(MyService.BROADCAST_ACTION));
        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            unregisterReceiver(broadcastReceiver);
            stopService(intent);
        }
    });

}

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

        updateUI(intent);
    }
};

private void updateUI(Intent intent) {
    int time = intent.getIntExtra("time", 0);

    Log.d("Hello", "Time " + time);

    int mins = time / 60;
    int secs = time % 60;
    timerValue.setText("" + mins + ":" + String.format("%02d", secs));
}

@Override
protected void onStop() {
    super.onStop();
    intent = new Intent(MainActivity.this, MyService.class);
    startService(intent);
    registerReceiver(broadcastReceiver, new IntentFilter(MyService.BROADCAST_ACTION));
}


}

MyService.java

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.widget.Toast;


 public class MyService extends Service
 {
private Intent intent;
public static final String BROADCAST_ACTION = "com.example.wajid.service";

private Handler handler = new Handler();
private long initial_time;
long timeInMilliseconds = 0L;

@Override
public void onCreate() {
    super.onCreate();
    initial_time = SystemClock.uptimeMillis();
    intent = new Intent(BROADCAST_ACTION);
    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

}

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        DisplayLoggingInfo();
        handler.postDelayed(this, 1000); // 1 seconds
    }
};

private void DisplayLoggingInfo() {

    timeInMilliseconds = SystemClock.uptimeMillis() - initial_time;

    int timer = (int) timeInMilliseconds / 1000;
    intent.putExtra("time", timer);
    sendBroadcast(intent);

}

@Override
public void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(sendUpdatesToUI);

}



@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


}

1 个答案:

答案 0 :(得分:1)

为什么要运行计时器?相反,只需将登记时间保存在共享首选项中即可。退房时,可以比较两者并计算相关时间。

如果您担心用户可能会尝试操作本地设备时钟,那么您可以使用网络时间而不是获取本地时间。