MainActivity.java代码如下:
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView tv = null;
private Button setTime,cancelTime;
private Calendar c = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//判断是否是从点击通知触发的
String alarm = getIntent().getStringExtra("alarm");
if(alarm != null&&alarm.equals("alarm")){
//关闭音乐
stopService(new Intent("com.yqy.yqy_alarm.MUSIC"));
}
tv = (TextView)findViewById(R.id.tv);
setTime = (Button)findViewById(R.id.setAlarm); //设置闹钟
cancelTime = (Button)findViewById(R.id.cancelAlarm); //取消闹钟
//得到日历实例,主要是为了下面的获取时间
c = Calendar.getInstance();
setTime.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
c.setTimeInMillis(System.currentTimeMillis());
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
new TimePickerDialog(MainActivity.this, minute, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//是设置日历的时间,主要是让日历的年月日和当前同步
c.setTimeInMillis(System.currentTimeMillis());
//设置小时分钟,秒和毫秒都设置为0
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
int requestCode = 0;//闹钟的唯一标示
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("requestCode", requestCode);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, requestCode, intent, 0);
//得到AlarmManager实例
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//根据当前时间预设一个警报
// am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
/**
* 第一个参数是警报类型;第二个参数是第一次执行的延迟时间,可以延迟,也可以马上执行;第三个参数是重复周期为一天
* 这句话的意思是设置闹铃重复周期,也就是执行警报的间隔时间
*/
// am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(60*1000),
// (24*60*60*1000), pi);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000*60*5, pi);
String msg = hourOfDay+":"+minute;
tv.setText("当前设置的闹钟时间:"+msg);
}
}, hour, minute, true).show();
//上面的TimePickerDialog中的5个参数参考:http://blog.csdn.net/yang_hui1986527/article/details/6839342
}
});
cancelTime.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//取消闹钟
am.cancel(pi);
tv.setText("闹钟取消");
//取消闹钟的同时取消音乐
stopService(new Intent("com.yqy.yqy_alarm.MUSIC"));
}
});
}
}
AlarmReceiver闹钟接收器
首先得注册
原文地址:http://blog.csdn.net/u012885461/article/details/46013597