这是咱们闹钟记事本的闹钟界面

闹钟界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/clockbg"
android:orientation="vertical" >
<AnalogClock
android:id="@+id/analogClock"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<DigitalClock
android:id="@+id/dclock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="30px"
android:layout_y="32px"
android:gravity="center"
android:textSize="55sp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview2"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="记事闹钟设置为:" />
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/hello" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/setclock"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="设置闹钟"
android:textSize="18sp" />
<Button
android:id="@+id/unsetclock"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消闹钟"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
闹钟界面的代码
public class AlarmMainActivity extends Activity {
private TextView textView;
private Button setbtn, unsetbtn;
private static final int ButtonAlarm = 1;
// 铃声文件夹
private String setAlarmFloder = "/sdcard/music/alarms";
private static final String TEMP_1 = "temp_1";
Calendar c = Calendar.getInstance();
private static int timeID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_main);
textView = (TextView) findViewById(R.id.textview1);
setbtn = (Button) findViewById(R.id.setclock);
unsetbtn = (Button) findViewById(R.id.unsetclock);
// 获取SharedPreferences
SharedPreferences per = getSharedPreferences(TEMP_1,
MODE_WORLD_READABLE);
// 获得text内容
String text_1 = per.getString("text_1", "当前没有设置闹钟!");
// 在text中显示内容
textView.setText(text_1);
// 设置铃声按钮
// setbell = (Button)findViewById(R.id.btn3);
setbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 取得按下按钮的时间做为TimePickerDialog的默认值
c.setTimeInMillis(System.currentTimeMillis());
// 定义获取时间
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
// 跳出TimePickerDialog来设定时间
new TimePickerDialog(AlarmMainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
// 取得设定后的时间 秒跟毫秒设为0
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// 定闹钟设定时间到时要执行CallAlarm.class
Intent intent = new Intent(
AlarmMainActivity.this, CallAlarm.class);
PendingIntent sender = PendingIntent
.getBroadcast(
AlarmMainActivity.this,
1,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 获取AlarmManager
// AlarmManager.RTC_WAKEUP设定服务在系统休眠时一样会执行
//
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// set()设定的PendingIntent只会执行一次
am.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), sender);
// 显示设定的闹钟时间
String tmpS = format(hourOfDay) + ":"
+ format(minute);
textView.setText(tmpS);
// toas显示设定的时间
Toast.makeText(AlarmMainActivity.this,
"设定的时间为:" + tmpS, Toast.LENGTH_LONG)
.show();
}
}, mHour, mMinute, true).show();
}
});
// 取消闹钟
unsetbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AlarmMainActivity.this,
CallAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(
AlarmMainActivity.this, 0, intent, 0);
// 由AlarmManager中移除
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
textView.setText("当前没有设置闹钟!");
// 以Toast提示已删除设定,并更新显示的闹钟时间
Toast.makeText(AlarmMainActivity.this, "闹钟已经取消",
Toast.LENGTH_LONG).show();
}
});
}
private String format(int x) {
String s = "" + x;
if (s.length() == 1)
s = "0" + s;
return s;
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// 获取编译器
SharedPreferences.Editor editor = getSharedPreferences(TEMP_1,
MODE_WORLD_WRITEABLE).edit();
// 将text内容添加到编译器
editor.putString("text_1", textView.getText().toString());
// 提交编辑内容
editor.commit();
}
}
public class Alarmalert extends Activity {
private static Alarmalert alarmAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent1= new Intent(Alarmalert.this,PlayMusic.class);
startService(intent1);
//闹钟显示画面
new AlertDialog.Builder(Alarmalert.this).setTitle("闹钟响了!!!").setMessage("有重要记事待查看!!!")
.setPositiveButton("关闭闹钟", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent1= new Intent(Alarmalert.this,PlayMusic.class);
stopService(intent1);
Alarmalert.this.finish();
}
}).show();
}
private static Alarmalert getInstance(){
return alarmAlert;
}
}
public class CallAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i= new Intent(context,Alarmalert.class);
//默认的跳转类型,将Activity放到一个新的Task中
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}