這是咱們鬧鐘記事本的鬧鐘界面
![](http://static.javashuo.com/static/loading.gif)
鬧鐘界面的佈局
<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);
}
}