android闹钟(五):实现闹钟功能
闹钟主页面:AlarmView.java
package com.mytest.myclock; import java.util.Calendar; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.AttributeSet; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ShareActionProvider; import android.widget.TimePicker; import android.widget.Toast; public class AlarmView extends LinearLayout { public AlarmView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public AlarmView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public AlarmView(Context context) { super(context); // TODO Auto-generated constructor stub } private ListView alarmListView; private Button addAlarmButton; private ArrayAdapter<Alarm> adapter; private final static String KEY_ALARM_LIST = "clarm_list"; private AlarmManager alarmManager; @Override protected void onFinishInflate() { super.onFinishInflate(); alarmListView = (ListView) this.findViewById(R.id.lv_alarm); addAlarmButton = (Button) this.findViewById(R.id.btn_add_alarm); addAlarmButton.setOnClickListener(new AddAlarmOnClickListener()); adapter = new ArrayAdapter<AlarmView.Alarm>(getContext(), android.R.layout.simple_list_item_1); alarmListView.setAdapter(adapter); alarmListView.setOnItemLongClickListener(new ClarmOnItemLongClickListener()); readSaveCalarm(); alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); } private final class ClarmOnItemLongClickListener implements AdapterView.OnItemLongClickListener { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { //System.out.println("onItemLongClick=================>"); new AlertDialog.Builder(getContext()).setTitle("确认删除?").setNegativeButton("取消", null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.out.println("onItemLongClick->onClick:which:" + which); removeAlarm(position); } }).show(); return true; } } private final class AddAlarmOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { // Toast.makeText(getContext(), "add",Toast.LENGTH_SHORT).show(); addAlarm(); } } private void removeAlarm(int index) { Alarm alarm = adapter.getItem(index); adapter.remove(alarm); saveAlarm(); alarmManager.cancel(PendingIntent.getBroadcast(getContext(), alarm.getId(), new Intent(getContext(),AlarmReceiver.class), 0)); } /** * 保存设置的闹钟。覆盖式的,全部重新写入,清空原有的。 */ private void saveAlarm() { Editor edit = getContext().getSharedPreferences(AlarmView.class.getName(), Context.MODE_PRIVATE).edit(); StringBuffer alarmListStr = new StringBuffer(); for (int i = 0; i < adapter.getCount(); i++) { alarmListStr.append(adapter.getItem(i).getTime()).append(","); } edit.putString(KEY_ALARM_LIST, alarmListStr.toString()); edit.commit(); } /** * 读取保存的闹钟 */ private void readSaveCalarm() { SharedPreferences share = getContext().getSharedPreferences(AlarmView.class.getName(), Context.MODE_PRIVATE); String alarmListStr = share.getString(KEY_ALARM_LIST, ""); if (alarmListStr != null) { String[] alarmArr = alarmListStr.split(","); if (alarmArr != null) { for (String str : alarmArr) { System.out.println("alarm:" + str); if (str != null && !"".equals(str.trim())) { long time = Long.valueOf(str); adapter.add(new Alarm(time)); } } } } } /** * 添加闹钟 */ private void addAlarm() { Calendar curCal = Calendar.getInstance(); new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar alarmCal = Calendar.getInstance(); alarmCal.set(Calendar.HOUR_OF_DAY, hourOfDay); alarmCal.set(Calendar.MINUTE, minute); alarmCal.set(Calendar.SECOND, 0); // 如果设置时间小于当前时间,则往后推一天。 Calendar currentTime = Calendar.getInstance(); if (alarmCal.getTimeInMillis() <= currentTime.getTimeInMillis()) { alarmCal.setTimeInMillis(alarmCal.getTimeInMillis() + 24 * 60 * 60 * 1000); } // System.out.println(String.format("hourOfDay:%d,minute:%d", // hourOfDay,minute)); // 添加到adapter,显示到列表 Alarm alarm = new Alarm(alarmCal.getTimeInMillis()); adapter.add(alarm); //每隔10分钟 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.getTime(), 10 * 60 * 1000, PendingIntent .getBroadcast(getContext(), alarm.getId(), new Intent(getContext(), AlarmReceiver.class), 0)); // 保存到share中,下次启动再读取 saveAlarm(); } }, curCal.get(Calendar.HOUR_OF_DAY), curCal.get(Calendar.MINUTE), true).show(); } private class Alarm { private long time = 0; private String timeLabel = ""; private Calendar date; public Alarm(Long time) { this.time = time; Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); timeLabel = String.format("%d-%d %d:%d", cal.get(Calendar.MONTH + 1), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); } public long getTime() { return time; } public String getTimeLabel() { return timeLabel; } public Calendar getDate() { return date; } public int getId() { return (int) (getTime() / 1000 / 60); } @Override public String toString() { return getTimeLabel(); } } }
View Code
闹钟时间到,播放音乐的activity:PlayAlarmAty.java
package com.mytest.myclock; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; public class PlayAlarmAty extends Activity { private MediaPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm_music_play); player = MediaPlayer.create(this, R.raw.an_angel); player.start(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); finish(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); player.stop(); player.release(); } }
View Code
闹钟播放界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="@string/play_sound" /> </LinearLayout>
View Code
闹钟时间到,接收处理类AlarmReceiver.java
package com.mytest.myclock; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("AlarmReceiver.onReceive"); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); manager.cancel( PendingIntent.getBroadcast(context, getResultCode(), new Intent(context, AlarmReceiver.class), 0)); Intent it = new Intent(context, PlayAlarmAty.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); } }
View Code
AndroidMainfest.xml添加配置
<receiver android:name=".AlarmReceiver"></receiver> <activity android:name="PlayAlarmAty" android:screenOrientation="portrait"></activity>
View Code
另:在res中添加音乐文件。