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;
}
}
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();
}
}
<receiver android:name=".media.AlarmReceiver" android:process=":remote"/> 注意註冊