package com.king.android.media;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import com.king.android.R;
/**
* 描述:闹钟事件
* 作者:Andy.Liu
* 时间: 2012-7-23 上午08:03:24
**/
public class AlarmActivity extends Activity implements OnClickListener {
Calendar mCalendar;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCalendar = Calendar.getInstance();
mTextView = (TextView) findViewById(R.id.tv_info);
mTextView.setVisibility(View.VISIBLE);
Button btnSetAlarm = (Button) findViewById(R.id.btn_first);
btnSetAlarm.setText("设置闹钟");
btnSetAlarm.setVisibility(View.VISIBLE);
btnSetAlarm.setOnClickListener(this);
btnSetAlarm = (Button) findViewById(R.id.btn_second);
btnSetAlarm.setText("取消闹钟");
btnSetAlarm.setVisibility(View.VISIBLE);
btnSetAlarm.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_first:
setAlarm();
break;
case R.id.btn_second:
setCancel();
break;
}
}
private void setAlarm(){
mCalendar.setTimeInMillis(System.currentTimeMillis());
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int miu = mCalendar.get(Calendar.MINUTE);
new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
mCalendar.set(Calendar.MINUTE, minute);
mCalendar.set(Calendar.SECOND, 0);
mCalendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//设置闹钟
am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
//设置周期闹钟
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*10000, 24*60*60*1000, pendingIntent);
String tmps = "设置闹钟为"+format(hourOfDay)+":"+format(minute);
mTextView.setText(tmps);
}
}, hour, miu, true).show();
}
/**
*
*TODO:取消闹钟
*Author:Andy.Liu
*Create Time:2012-7-23 上午08:18:09
*TAG:
*Return:void
*/
private void setCancel(){
Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//取消闹钟
am.cancel(pendingIntent);
mTextView.setText(" 闹钟已经取消");
}
/**
*
*TODO:格式化字符串
*Author:Andy.Liu
*Create Time:2012-7-23 上午08:16:01
*TAG:@param x
*Return:void
*/
private String format(int x){
String s = x+"";
if(s.length()==1){
s="0"+s;
}
return s;
}
}
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import com.king.android.R;
/**
* 描述:闹钟事件
* 作者:Andy.Liu
* 时间: 2012-7-23 上午08:03:24
**/
public class AlarmActivity extends Activity implements OnClickListener {
Calendar mCalendar;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCalendar = Calendar.getInstance();
mTextView = (TextView) findViewById(R.id.tv_info);
mTextView.setVisibility(View.VISIBLE);
Button btnSetAlarm = (Button) findViewById(R.id.btn_first);
btnSetAlarm.setText("设置闹钟");
btnSetAlarm.setVisibility(View.VISIBLE);
btnSetAlarm.setOnClickListener(this);
btnSetAlarm = (Button) findViewById(R.id.btn_second);
btnSetAlarm.setText("取消闹钟");
btnSetAlarm.setVisibility(View.VISIBLE);
btnSetAlarm.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_first:
setAlarm();
break;
case R.id.btn_second:
setCancel();
break;
}
}
private void setAlarm(){
mCalendar.setTimeInMillis(System.currentTimeMillis());
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int miu = mCalendar.get(Calendar.MINUTE);
new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
mCalendar.set(Calendar.MINUTE, minute);
mCalendar.set(Calendar.SECOND, 0);
mCalendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//设置闹钟
am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
//设置周期闹钟
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*10000, 24*60*60*1000, pendingIntent);
String tmps = "设置闹钟为"+format(hourOfDay)+":"+format(minute);
mTextView.setText(tmps);
}
}, hour, miu, true).show();
}
/**
*
*TODO:取消闹钟
*Author:Andy.Liu
*Create Time:2012-7-23 上午08:18:09
*TAG:
*Return:void
*/
private void setCancel(){
Intent intent = new Intent(AlarmActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//取消闹钟
am.cancel(pendingIntent);
mTextView.setText(" 闹钟已经取消");
}
/**
*
*TODO:格式化字符串
*Author:Andy.Liu
*Create Time:2012-7-23 上午08:16:01
*TAG:@param x
*Return:void
*/
private String format(int x){
String s = x+"";
if(s.length()==1){
s="0"+s;
}
return s;
}
}
package com.king.android.media;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* 描述:闹钟广播
* 作者:Andy.Liu
* 时间: 2012-7-23 上午08:00:38
**/
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "你设置的时间到了", Toast.LENGTH_LONG).show();
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* 描述:闹钟广播
* 作者:Andy.Liu
* 时间: 2012-7-23 上午08:00:38
**/
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "你设置的时间到了", Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".media.AlarmReceiver" android:process=":remote"/> 注意注册