首页 > 代码库 > Android源码之简单定时器

Android源码之简单定时器

public class MainActivity extends Activity implements OnClickListener {
 
    private Button bt_main_start;
    private Button bt_main_over;
    private EditText et_main_inputTime;
    private Intent mService;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mService = new Intent(this, MyService.class);
        initView();
    }
 
    private void initView() {
        bt_main_start = (Button) findViewById(R.id.bt_main_start);
        bt_main_over = (Button) findViewById(R.id.bt_main_over);
        et_main_inputTime = (EditText) findViewById(R.id.et_main_inputTime);
         
        bt_main_start.setOnClickListener(this);
        bt_main_over.setOnClickListener(this);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_main_start:
            String minute = et_main_inputTime.getText().toString().trim();
            if(TextUtils.isEmpty(minute)){
                return;
            }
            mService.putExtra("minute", Integer.parseInt(minute));
            startService(mService);
            break;
        case R.id.bt_main_over:
            stopService(mService);
            break;
 
        }
    }
}

Android源码之简单定时器