简单实现Android闹钟程序 附源码

这个应用实现了简单的闹钟功能,关闭程序的状态中也可以进行闹钟的提醒。

这里遗留了一个问题:

如果我通过应用本身关闭程序,是可以实现我上述所说的功能,但是如果我在手机进程管理中实现应用程序的关闭操作,这个提醒就不起作用了,我很疑惑的是,我通过应用程序本身关闭了后,在进程中也是查看不到这个应用了的。所以哪位知道的帮留个言指点下,谢谢。

ClockDemo.java

package com.example.clock;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class ClockDemo extends Activity {

 private Button mSet;
 Calendar mCalendar = Calendar.getInstance();

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 ObjectPool.mAlarmHelper = new AlarmHelper(this);
 mSet = (Button) findViewById(R.id.mSet);
 setListener();
 }

 public void setListener() {
 mSet.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 mCalendar.setTimeInMillis(System.currentTimeMillis());
 int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
 int mMinute = mCalendar.get(Calendar.MINUTE);
 new TimePickerDialog(ClockDemo.this,
  new TimePickerDialog.OnTimeSetListener() {
  public void onTimeSet(TimePicker view,
   int hourOfDay, int minute) {
  mCalendar.setTimeInMillis(System
   .currentTimeMillis());
  mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
  mCalendar.set(Calendar.MINUTE, minute);
  mCalendar.set(Calendar.SECOND, 0);
  mCalendar.set(Calendar.MILLISECOND, 0);
  ObjectPool.mAlarmHelper.openAlarm(32, "ddd",
   "ffff", mCalendar.getTimeInMillis());
  }
  }, mHour, mMinute, true).show();
 }
 });
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 if (keyCode == KeyEvent.KEYCODE_BACK) {
 showBackDialog();
 return true;
 }
 return super.onKeyDown(keyCode, event);
 }

 /** Give the tip when exit the application. */
 public void showBackDialog() {
 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("提示")
 .setIcon(R.drawable.icon)
 .setMessage("是否退出?")
 .setPositiveButton("sure",
  new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog,
   int which) {
  System.exit(0);
  android.os.Process
   .killProcess(android.os.Process.myPid());

  dialog.dismiss();
  }
  })
 .setNegativeButton("cancel",
  new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog,
   int which) {
  dialog.dismiss();
  }
  });
 AlertDialog ad = builder.create();
 ad.show();
 }

}
 

你可能感兴趣的:(简单实现Android闹钟程序 附源码)