package com.fit.timer;
import java.util.Date;
import java.util.TimerTask;
import java.util.Timer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class StopwatchActivity extends Activity {
/** Called when the activity is first created. */
private Button start_pause, reset;
protected static EditText edit;
protected static int time_input = 0;
protected static boolean flag = false;
protected static boolean pauseflag = false;
protected static int time_now = 0;
protected static TextView timeView;
Timer timer= new Timer();
TimerTask task = null;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg)
{
switch(msg.what)
{
case 1:
{
if(time_now == time_input)
{
flag = false;
task.cancel();
// 给用户提示
showDialog();
time_now = 0;
pauseflag = false;
}
else
{
time_now ++;
timeView.setText(String.valueOf(time_now));
}
break;
}
}
};
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.stopwatch_activity);
timeView=(TextView) findViewById(R.id.timeview);
start_pause = (Button) findViewById(R.id.start_pause);
reset = (Button) findViewById(R.id.reset);
edit = (EditText) findViewById(R.id.edit);
start_pause.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(time_now == 0 && pauseflag == false)
{
flag = true;
time_input = Integer.parseInt(edit.getText().toString());
task=new TimerTask() {
@Override
public void run()
{
Message message=new Message();
message.what=1;
handler.sendMessage(message);
if(time_now == time_input)
{
System.gc();
}
}
};
timer.scheduleAtFixedRate(task, new Date(), 1000);
}
else if(pauseflag)
{
flag = false;
task.cancel();
System.gc();
}
else
{
flag = true;
task=new TimerTask() {
@Override
public void run()
{
Message message=new Message();
message.what=1;
handler.sendMessage(message);
if(time_now == time_input)
{
System.gc();
}
}
};
timer.scheduleAtFixedRate(task, new Date(), 1000);
}
pauseflag = !pauseflag;
}
});
reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if( time_now != 0)
{
flag = false;
task.cancel();
System.gc();
pauseflag = false;
}
time_now = 0;
timeView.setText(String.valueOf(time_now));
}
});
}
@Override
public void onResume()
{
super.onResume();
if((time_now != 0)&&(time_input != 0)&&(time_input == time_now-1))
{
timeView.setText(String.valueOf((StopwatchActivity.time_now-1)));
edit.setText(String.valueOf(StopwatchActivity.time_input));
time_now = 0;
flag = false;
pauseflag = false;
showDialog();
stopService(new Intent("com.fit.timer.action.StopwatchService"));
}
}
@Override
public void onStop()
{
super.onStop();
if(flag == true && time_now != 0)
{
task.cancel();
System.gc();
startService(new Intent("com.fit.timer.action.StopwatchService"));
Log.i("我要启动后台服务了","StopwatchService");
}
}
protected void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("Notification").setMessage("Time is over.")
.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
安卓实现简单定时器功能源码
共44个文件
class:18个
png:8个
java:4个
4星 · 超过85%的资源 需积分: 10 65 下载量 49 浏览量
2015-03-22
19:28:15
上传
评论
收藏 132KB ZIP 举报
温馨提示
实现的任务要求: 计算花费多长时间 允许用户开始,停止,和暂停计时器 应用service 你可以增加输入时间或者显示剩余时间的功能 恰当使用notification
资源推荐
资源详情
资源评论
收起资源包目录
简单定时器功能.zip (44个子文件)
proj_mytimer
bin
classes.dex 12KB
res
crunch
drawable-ldpi
icon.png 2KB
mipmap
ic_launcher.png 14KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
jarlist.cache 120B
dexedLibs
annotations-868359fa00146b4cff1ea54f03994680.jar 910B
classes
com
fit
timer
BuildConfig.class 335B
R$mipmap.class 386B
R$layout.class 393B
R$id.class 463B
R$drawable.class 385B
StopwatchActivity$1.class 1KB
StopwatchService.class 2KB
StopwatchActivity$2.class 2KB
R$attr.class 325B
StopwatchActivity$4.class 835B
R.class 541B
StopwatchActivity$2$2.class 1KB
StopwatchActivity$2$1.class 1KB
R$string.class 412B
StopwatchService$1.class 2KB
StopwatchActivity$3.class 1KB
StopwatchActivity.class 4KB
StopwatchService$MyThread.class 1KB
resources.ap_ 25KB
proj_mytimer.apk 32KB
AndroidManifest.xml 885B
res
drawable-ldpi
icon.png 2KB
mipmap
ic_launcher.png 14KB
drawable-hdpi
icon.png 4KB
values
strings.xml 172B
drawable-mdpi
icon.png 3KB
layout
stopwatch_activity.xml 2KB
gen
com
fit
timer
BuildConfig.java 155B
R.java 1008B
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 208B
src
com
fit
timer
StopwatchActivity.java 5KB
StopwatchService.java 3KB
.project 848B
.classpath 475B
project.properties 562B
AndroidManifest.xml 885B
共 44 条
- 1
资源评论
- bugmenot12016-02-16修改了一下版本可以运行了,还不错。黍离麦秀2018-08-20谢谢关注
黍离麦秀
- 粉丝: 8
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功